80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
import os
|
|
import asyncio
|
|
import discord
|
|
|
|
from libs.Db import Db
|
|
from libs.BotLog import BotLog
|
|
from libs.Guilds import Guilds
|
|
from libs.Channels import Channels
|
|
from discord.ext import commands
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
token = os.getenv("DISCORD_TOKEN")
|
|
prefix = os.getenv("COMMAND_PREFIX")
|
|
guild_id = os.getenv("GUILD_ID")
|
|
dev_mode = os.getenv("DEV_MODE")
|
|
db_conn_str = os.getenv("DB_CONN_STR")
|
|
|
|
bot_intents = discord.Intents.default()
|
|
bot_intents.message_content = True
|
|
bot_intents.members = True
|
|
|
|
|
|
class FunkyBot(commands.Bot):
|
|
def __init__(self):
|
|
super().__init__(
|
|
command_prefix=prefix,
|
|
intents=bot_intents
|
|
)
|
|
self.log_handler = BotLog("bot.log")
|
|
self.db = Db(db_conn_str, True)
|
|
self.guild = None
|
|
self.guild_id = guild_id
|
|
self.dev_mode = dev_mode == "1"
|
|
|
|
async def on_ready(self):
|
|
print(f"Logged in as {self.user.name}")
|
|
self.guild = await Guilds().get_guild(self)
|
|
await self.tree.sync(guild=self.guild)
|
|
|
|
async def async_cleanup(self):
|
|
channels_to_purge = {
|
|
"add-roles": "FunkyBot is currently sleeping. Please try again once he has awakened.",
|
|
"live-penguins": "The FunkyBot Twitch Live Notification System is currently offline. Live Alerts will resume later."
|
|
}
|
|
|
|
for channel, message in channels_to_purge.items():
|
|
the_channel = await Channels().get_channel(self.guild, channel)
|
|
await the_channel.purge()
|
|
if message is not None:
|
|
await the_channel.send(message)
|
|
...
|
|
|
|
async def close(self):
|
|
await self.async_cleanup()
|
|
await super().close()
|
|
|
|
async def on_member_join(self, member):
|
|
channel = discord.utils.get(self.guild.channels, name="general")
|
|
role = discord.utils.get(await self.guild.fetch_roles(), name="Chinstrap Penguins")
|
|
if role is not None:
|
|
if role not in member.roles:
|
|
await member.add_roles(role)
|
|
await channel.send(f"Another Penguin has joined the Collective. Everyone say hi to {member.mention}!")
|
|
|
|
async def load_cogs(self):
|
|
for filename in os.listdir("./cogs"):
|
|
if filename.endswith(".py"):
|
|
await self.load_extension(f"cogs.{filename[:-3]}")
|
|
print(f"Loaded {filename[:-3]}")
|
|
|
|
|
|
async def main():
|
|
bot = FunkyBot()
|
|
async with bot:
|
|
await bot.load_cogs()
|
|
await bot.start(token)
|
|
|
|
asyncio.run(main())
|