2024-08-09 22:28:02 +01:00
|
|
|
"""
|
|
|
|
matchy.py - Discord bot that matches people into groups
|
|
|
|
"""
|
2024-08-08 23:32:52 +01:00
|
|
|
import logging
|
2024-08-09 22:28:02 +01:00
|
|
|
import discord
|
2024-08-13 20:12:48 +01:00
|
|
|
from discord.ext import commands
|
2024-08-10 10:45:44 +01:00
|
|
|
import config
|
2024-08-13 20:12:48 +01:00
|
|
|
import state
|
|
|
|
import cog
|
|
|
|
import match_button
|
2024-08-10 10:06:13 +01:00
|
|
|
|
2024-08-13 20:12:48 +01:00
|
|
|
State = state.load_from_file()
|
2024-08-11 17:53:37 +01:00
|
|
|
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-08 23:32:52 +01:00
|
|
|
logger = logging.getLogger("matchy")
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
2024-08-07 22:15:39 +01:00
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
2024-08-07 23:23:52 +01:00
|
|
|
intents.members = True
|
2024-08-09 22:12:58 +01:00
|
|
|
bot = commands.Bot(command_prefix='$',
|
|
|
|
description="Matchy matches matchees", intents=intents)
|
|
|
|
|
2024-08-07 22:15:39 +01:00
|
|
|
|
2024-08-11 11:37:20 +01:00
|
|
|
@bot.event
|
|
|
|
async def setup_hook():
|
2024-08-13 20:12:48 +01:00
|
|
|
await bot.add_cog(cog.MatchyCog(bot, State))
|
|
|
|
# TODO: This line feels like it should be in the cog?
|
|
|
|
bot.add_dynamic_items(match_button.DynamicGroupButton)
|
2024-08-11 11:37:20 +01:00
|
|
|
|
|
|
|
|
2024-08-08 00:09:30 +01:00
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2024-08-13 20:12:48 +01:00
|
|
|
logger.info("Logged in as %s", bot.user.name)
|
2024-08-07 23:23:52 +01:00
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-10 10:06:13 +01:00
|
|
|
def owner_only(ctx: commands.Context) -> bool:
|
|
|
|
"""Checks the author is an owner"""
|
2024-08-11 17:53:37 +01:00
|
|
|
return State.get_user_has_scope(ctx.message.author.id, state.AuthScope.OWNER)
|
2024-08-10 10:06:13 +01:00
|
|
|
|
|
|
|
|
2024-08-08 19:45:53 +01:00
|
|
|
@bot.command()
|
2024-08-09 00:32:59 +01:00
|
|
|
@commands.dm_only()
|
2024-08-10 10:06:13 +01:00
|
|
|
@commands.check(owner_only)
|
2024-08-09 16:34:11 +01:00
|
|
|
async def sync(ctx: commands.Context):
|
2024-08-09 22:08:05 +01:00
|
|
|
"""Handle sync command"""
|
2024-08-11 17:53:37 +01:00
|
|
|
msg = await ctx.reply("Reloading state...", ephemeral=True)
|
|
|
|
global State
|
2024-08-13 20:12:48 +01:00
|
|
|
State = state.load_from_file()
|
2024-08-11 17:53:37 +01:00
|
|
|
logger.info("Reloaded state")
|
2024-08-09 16:34:11 +01:00
|
|
|
|
2024-08-09 22:08:05 +01:00
|
|
|
await msg.edit(content="Syncing commands...")
|
2024-08-08 19:45:53 +01:00
|
|
|
synced = await bot.tree.sync()
|
2024-08-09 22:28:02 +01:00
|
|
|
logger.info("Synced %s command(s)", len(synced))
|
2024-08-09 16:34:11 +01:00
|
|
|
|
|
|
|
await msg.edit(content="Done!")
|
2024-08-08 19:45:53 +01:00
|
|
|
|
2024-08-09 22:12:58 +01:00
|
|
|
|
2024-08-10 00:01:43 +01:00
|
|
|
@bot.command()
|
|
|
|
@commands.dm_only()
|
2024-08-10 10:06:13 +01:00
|
|
|
@commands.check(owner_only)
|
2024-08-10 00:01:43 +01:00
|
|
|
async def close(ctx: commands.Context):
|
|
|
|
"""Handle restart command"""
|
|
|
|
await ctx.reply("Closing bot...", ephemeral=True)
|
|
|
|
logger.info("Closing down the bot")
|
|
|
|
await bot.close()
|
|
|
|
|
|
|
|
|
2024-08-09 23:14:42 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
handler = logging.StreamHandler()
|
2024-08-11 22:07:43 +01:00
|
|
|
bot.run(config.Config.token, log_handler=handler, root_logger=True)
|