-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiscordBot.py
More file actions
101 lines (80 loc) · 3.42 KB
/
Copy pathDiscordBot.py
File metadata and controls
101 lines (80 loc) · 3.42 KB
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
# Python Libaries
from datetime import datetime, timezone
import logging
from logging.handlers import RotatingFileHandler
import sys
# Discord Libraries
import discord
from discord.ext import commands
# Local Includes
import Shared
class Wes(commands.Bot):
def __init__(self, *args, **kwargs):
self.killed = False
self.start_timestamp = datetime.now(timezone.utc)
self.log = self.create_log("Bot")
super(Wes, self).__init__(*args, **kwargs)
# Returns the days, hours, minutes, and seconds since the bot was last initialized
def calculate_uptime(self):
uptime = (datetime.now(timezone.utc) - self.start_timestamp)
hours, remainder = divmod(uptime.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return uptime.days, hours, minutes, seconds
# Creates or returns a log file of the given name.
def create_log(self, name):
logger = logging.getLogger(name)
# Only create file handlers if the log doesn't have any, not on reload
if not logger.hasHandlers():
logger.setLevel(logging.INFO)
# Create <Cog>.log log file
fh = RotatingFileHandler(f"Logs/{name}.log", "a+", maxBytes=1000000, backupCount=1) # one file, max size of 4mb
fh.setLevel(logging.INFO)
fh.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(filename)s %(lineno)d %(message)s"))
logger.addHandler(fh)
return logger
async def sync_command_trees(self):
OTH_GUILD = bot.get_guild(Shared.OTH_GUILD_ID)
KK_GUILD = bot.get_guild(Shared.KK_GUILD_ID)
guilds = [None, OTH_GUILD, KK_GUILD]
for guild in guilds:
if guild == None:
self.log.info("Global Commands...")
else:
self.log.info(f"{guild.name} Commands...")
# Print commands in this group
for command in self.tree.get_commands(guild=guild):
self.log.info(f"\t{command.name}")
# self.tree.clear_commands(guild=guild)
await self.tree.sync(guild=guild)
self.log.info("...Synced")
async def setup_hook(self):
for cog in Shared.all_cogs:
await bot.load_extension(cog)
# https://discordpy.readthedocs.io/en/stable/intents.html
intents = discord.Intents.default()
intents.members = True # Needed for high accuracy cache on Guild.members and Member.roles (used for OTH roles)
bot = Wes(command_prefix="!", case_insensitive=True, help_command=None, intents=intents, heartbeat_timeout=120)
@bot.event
async def on_connect():
Shared.start_timestamp = datetime.now(timezone.utc)
await bot.change_presence(activity=discord.Game(name="NHL '94"))
await bot.user.edit(username="Wes McCauley")
bot.log.info("Bot started.")
@bot.event
async def on_disconnect():
days, hours, minutes, seconds = bot.calculate_uptime()
bot.log.info(f"Bot disconnected after {days} day(s), {hours} hour(s), {minutes} minute(s), {seconds} seconds(s).")
@bot.event
async def on_ready():
await bot.sync_command_trees()
bot.log.info("Bot ready.")
if __name__ == "__main__":
if len(sys.argv) > 1:
if sys.argv[1] == "test":
bot.run(Shared.config["beta_discord_token"], reconnect=True)
else:
bot.run(Shared.config["discord_token"], reconnect=True)
# Hang if bot was killed by command to prevent recreation by pm2
if bot.killed:
while True:
continue