-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
115 lines (89 loc) · 3.76 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# IMPORT START
import logging
from os import getenv
from pathlib import Path
from platform import platform
from time import time
import discord
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from colorama import Fore, Style
from discord import Game, Intents, Message, Status
from discord.ext.commands import Bot as bonzoBot
from discord.ext.commands import Cog, when_mentioned_or
from dotenv import load_dotenv
from config import OWNER_IDS, PREFIX
print("loading...")
print("-----------------------------")
print(Fore.GREEN + "libs imported")
print("-----------------------------")
print(f"Powered on {discord.__name__} | version {discord.__version__}")
print("-----------------------------" + Fore.MAGENTA)
# IMPORT END
load_dotenv() # загружает файл env
logger = logging.getLogger("discord")
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(
filename="discord.log", encoding="utf-8", mode="a")
handler.setFormatter(
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
)
logger.addHandler(handler)
class Bot(bonzoBot):
def __init__(self):
intents = Intents.default()
intents.presences = True
intents.members = True
# it is mandatory to request access to this intent explicitly via discord dev portal
intents.message_content = False
super().__init__(
command_prefix=when_mentioned_or(PREFIX),
help_command=None,
intents=intents,
owner_ids=OWNER_IDS,
)
self.game = Game("/init | ON REWORK")
self.scheduler = AsyncIOScheduler()
self.startTime = None
async def setup_hook(self):
await self.cogsLoad()
# await self.tree.sync()
async def cogsLoad(self):
cmds = [x.stem for x in Path(
'./commands').iterdir() if x.suffix == '.py' and x.is_file()]
total = len(cmds)
for curr, cmd in enumerate(cmds, start=1):
try:
await self.load_extension(f"commands.{cmd}")
print(f"cog {cmd} load, {curr}/{total}")
except Exception as error: # something in cog wrong
print(f"error in cog {cmd}, {curr}/{total} | {error}")
logging.error(f"cog filename not load: {error}")
def run(self):
self.startTime = time() # таймштамп: код успешно прочитан
print(
"/",
"initialization file has been successfully read. starting up bonzo...",
sep="\n",
)
super().run(getenv("TOKEN")) # берёт переменную TOKEN из .env
@Cog.listener()
async def on_ready(self):
# бот меняет свой статус именно благодаря этой команде (и "играет" в "игру")
await self.change_presence(status=Status.online, activity=self.game)
self.scheduler.start()
endTime = time() - self.startTime
print(
f"/ \n bonzo has been successfully initialized on {platform()} \n timestamp delta is: {round(endTime, 3)}s \n discord latency is: {(round(self.latency, 3))}s \n / \n end."
)
@Cog.listener()
async def on_resumed(self):
print(Fore.GREEN + "-----------------------\nbot resumed\n-----------------------" + Style.RESET_ALL)
logging.info("bot resumed")
@Cog.listener()
async def on_disconnect(self):
print(Fore.RED + "-----------------------\nbot disconnected or connection failed\n-----------------------" + Style.RESET_ALL)
logging.warning("bot disconnected")
@Cog.listener()
async def on_connect(self):
print(Fore.GREEN + "-----------------------\nbot connected\n-----------------------" + Style.RESET_ALL)
logging.info("bot connected")