From b739fca703ec17e7db5082bf6c4f8e050ab91fe6 Mon Sep 17 00:00:00 2001 From: Marko259 Date: Sat, 4 Sep 2021 00:09:42 +0200 Subject: [PATCH] Cleaned up --- bot.py | 2 +- cogs/admin.py | 41 ++++------------ cogs/announcement.py | 100 ---------------------------------------- cogs/staffing.py | 34 +++----------- cogs/tasks.py | 4 +- cogs/update_messages.py | 4 +- helpers/config.py | 16 ------- helpers/event.py | 2 +- helpers/message.py | 11 +---- 9 files changed, 24 insertions(+), 190 deletions(-) delete mode 100644 cogs/announcement.py diff --git a/bot.py b/bot.py index 2837c72..212265f 100644 --- a/bot.py +++ b/bot.py @@ -16,7 +16,7 @@ intents = discord.Intents.all() -bot = commands.Bot(command_prefix=config.PREFIXES, description=config.DESCRIPTION, intents=intents, help_command=None, case_insensitive=True) +bot = commands.Bot(command_prefix='/', description=config.DESCRIPTION, intents=intents, help_command=None, case_insensitive=True) slash = SlashCommand(bot, sync_commands=True) diff --git a/cogs/admin.py b/cogs/admin.py index 2df1bb6..8d50f16 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -3,24 +3,20 @@ from discord.ext import commands, tasks from discord_slash import cog_ext, SlashContext -from helpers.message import roles, embed -from helpers.config import COGS_LOAD, GUILD_ID, BOT_CHANNEL, STAFFING_INTERVAL, DEBUG +from helpers.message import staff_roles, embed +from helpers.config import COGS_LOAD, GUILD_ID, DEBUG class AdminCog(commands.Cog): def __init__(self, bot): self.bot = bot - self.reload_staffing.start() - - def cog_unload(self): - self.reload_staffing.cancel() guild_ids = [GUILD_ID] # Hidden means it won't show up on the default help. @cog_ext.cog_slash(name="load", guild_ids=guild_ids, description="Command which Loads a Module.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def load(self, ctx: SlashContext, *, cog: str): """ Command which Loads a Module. @@ -34,7 +30,7 @@ async def load(self, ctx: SlashContext, *, cog: str): await ctx.send('**`SUCCESS`**') @cog_ext.cog_slash(name="unload", guild_ids=guild_ids, description="Command which Unloads a Module.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def unload(self, ctx: SlashContext, *, cog: str): """ Command which Unloads a Module. @@ -48,7 +44,7 @@ async def unload(self, ctx: SlashContext, *, cog: str): await ctx.send('**`SUCCESS`**') @cog_ext.cog_slash(name="reload", guild_ids=guild_ids, description="Command which Reloads a Module.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def reload(self, ctx: SlashContext, *, cog: str): """ Command which Reloads a Module. @@ -62,24 +58,8 @@ async def reload(self, ctx: SlashContext, *, cog: str): else: await ctx.send('**`SUCCESS`**') - @tasks.loop(seconds=STAFFING_INTERVAL) - async def reload_staffing(self): - """ - Command which Reloads a Module. - """ - await self.bot.wait_until_ready() - now = datetime.datetime.now() - channel = self.bot.get_channel(int(BOT_CHANNEL)) - - try: - if now.weekday() == 0 and now.hour == 23 and 00 <= now.minute <= 00: - self.bot.unload_extension(COGS_LOAD["staffing"]) - self.bot.load_extension(COGS_LOAD["staffing"]) - except Exception as e: - await channel.send(f'**`ERROR:`** {type(e).__name__} - {e}') - @cog_ext.cog_slash(name="cogs", guild_ids=guild_ids, description="Command which sends a message with all available cogs.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def cogs(self, ctx: SlashContext): """ Command which sends a message with all available cogs. @@ -95,7 +75,7 @@ async def cogs(self, ctx: SlashContext): await ctx.send(embed=msg) @cog_ext.cog_slash(name="ping", guild_ids=guild_ids, description="Function sends pong if member has any of the admin roles.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def ping(self, ctx: SlashContext): """ Function sends pong if member has any of the admin roles @@ -105,7 +85,7 @@ async def ping(self, ctx: SlashContext): await ctx.send('Pong') @cog_ext.cog_slash(name="say", guild_ids=guild_ids, description="Bot sends a specific message sent by user.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def say(self, ctx: SlashContext, *, content: str) -> None: """ Bot sends a specific message sent by user @@ -113,11 +93,11 @@ async def say(self, ctx: SlashContext, *, content: str) -> None: :param content: :return None: """ - msg = await ctx.send("Message is being generated", delete_after=5) + await ctx.send("Message is being generated", delete_after=5) await ctx.send(content) @cog_ext.cog_slash(name="delete", guild_ids=guild_ids, description="Function deletes specific amount of messages.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def delete(self, ctx, *, number: int = 0): """ Function deletes specific amount of messages @@ -132,7 +112,6 @@ async def delete(self, ctx, *, number: int = 0): async for msg in ctx.channel.history(limit=number): msg_delete.append(msg) - """await ctx.message.channel.delete_messages(msg_delete)""" msgs = await ctx.send("Deleting messages") await msgs.channel.purge(limit=number) except Exception as exception: diff --git a/cogs/announcement.py b/cogs/announcement.py deleted file mode 100644 index 658a06b..0000000 --- a/cogs/announcement.py +++ /dev/null @@ -1,100 +0,0 @@ -from discord.ext import commands -from discord_slash import cog_ext, SlashContext -from helpers.message import roles -from helpers.config import GUILD_ID - -class AnnouncementCog(commands.Cog): - - def __init__(self, bot): - self.bot = bot - - guild_ids = [GUILD_ID] - - @cog_ext.cog_slash(name="announce", guild_ids=guild_ids, description="Function sends announcement message in specific channel.") - @commands.has_any_role(*roles()) - async def announce(self, ctx) -> None: - - """ - Function sends announcement message in specific channel - :param ctx: - :return: - """ - - channels = await self._get_channels(ctx) - roles = await self._get_roles(ctx) - message = await self._get_message(ctx) - - format_message = "" - - for role in roles: - format_message += role.mention - - if format_message != "": - format_message += "\n" - - format_message += message - - for channel in channels: - await channel.send(format_message) - - async def _get_channels(self, ctx): - """ - Function gets channels where it should send the announcement - :param ctx: - :return: - """ - try: - await ctx.send('Awesome! Where would you like to post the announcement?') - - message = await self.bot.wait_for('message', timeout=60, check=lambda - message: message.author == ctx.author and ctx.channel == message.channel) - - if len(message.channel_mentions) < 1: - await ctx.send('Announcement canceled.') - raise ValueError - - return message.channel_mentions - except Exception as exception: - await ctx.send(exception) - raise exception - - async def _get_roles(self, ctx): - """ - Function gets roles which should be pinged on announcement - :param ctx: - :return: - """ - try: - await ctx.send('Which roles should be tagged?') - - message = await self.bot.wait_for('message', timeout=60, check=lambda - message: message.author == ctx.author and ctx.channel == message.channel) - - return message.role_mentions - except Exception as exception: - await ctx.send(exception) - raise exception - - async def _get_message(self, ctx): - """ - Function gets a message that'll be included in announcement - :param ctx: - :return: - """ - try: - await ctx.send('Message?') - message = await self.bot.wait_for('message', timeout=60, check=lambda - message: message.author == ctx.author and ctx.channel == message.channel) - - if len(message.content) < 1: - await ctx.send('Announcement cancelled. No message was provided.') - raise ValueError - - return message.content - except Exception as exception: - await ctx.send(exception) - raise exception - - -def setup(bot): - bot.add_cog(AnnouncementCog(bot)) diff --git a/cogs/staffing.py b/cogs/staffing.py index a860870..5fb1329 100644 --- a/cogs/staffing.py +++ b/cogs/staffing.py @@ -148,29 +148,15 @@ async def updatestaffing(self, ctx, title): if message.content == options[0]: newtitle = await self._get_title(ctx) - cursor.execute( - 'UPDATE staffing, positions SET staffing.title = %s, positions.title = %s WHERE staffing.title = %s and positions.title = %s', - ( - newtitle, - newtitle, - title, - title - ) - ) + cursor.execute(f'UPDATE staffing, positions SET staffing.title = {newtitle}, positions.title = {newtitle} WHERE staffing.title = {title} and positions.title = {title}') mydb.commit() title = newtitle await self._updatemessage(title) - await ctx.send(f'Title updated to - {newtitle}') + await ctx.send(f'Title updated to - {title}') elif message.content == options[1]: newdate = await self._get_date(ctx) - cursor.execute( - 'UPDATE staffing SET date = %s WHERE title = %s', - ( - newdate, - title - ) - ) + cursor.execute(f'UPDATE staffing SET date = {newdate} WHERE title = {title}') mydb.commit() await self._updatemessage(title) formatted_date = newdate.strftime("%A %d/%m/%Y") @@ -180,13 +166,7 @@ async def updatestaffing(self, ctx, title): newdescription = await self._get_description(ctx) newdescription = newdescription + "\n\nTo book a position, write `/book`, press TAB and then write the callsign.\nTo unbook a position, use `/unbook`.\n\n" - cursor.execute( - 'UPDATE staffing SET description = %s WHERE title = %s', - ( - newdescription, - title - ) - ) + cursor.execute(f'UPDATE staffing SET description = {newdescription} WHERE title = {title}') mydb.commit() await self._updatemessage(title) await ctx.send(f'Event description/staffing message has been updated to:\n{newdescription}') @@ -306,7 +286,7 @@ async def book(self, ctx, position): await self._updatemessage(title[0]) await ctx.send(f"<@{usernick}> Confirmed booking for position `{position.upper()}` for event `{title[0]}`", delete_after=5) else: - await ctx.send(f"<@{usernick}> The bot could not found the position you tried to book.") + await ctx.send(f"<@{usernick}> The bot could not find the position you tried to book.") else: await ctx.send(f"<@{usernick}> Please use the correct channel", delete_after=5) @@ -461,8 +441,8 @@ async def _get_description(self, ctx): :return: """ try: - await ctx.send('Staffing message? **FYI this command expires in 3 minutes*') - message = await self.bot.wait_for('message', timeout=180, check=lambda message: message.author == ctx.author and ctx.channel == message.channel) + await ctx.send('Staffing message? **FYI this command expires in 5 minutes*') + message = await self.bot.wait_for('message', timeout=300, check=lambda message: message.author == ctx.author and ctx.channel == message.channel) if len(message.content) < 1: await ctx.send('Setup cancelled. No message was provided.') diff --git a/cogs/tasks.py b/cogs/tasks.py index fbb341c..b3f0e31 100644 --- a/cogs/tasks.py +++ b/cogs/tasks.py @@ -7,7 +7,7 @@ from discord.ext import commands, tasks from discord_slash import cog_ext, SlashContext from dotenv import load_dotenv -from helpers.message import roles +from helpers.message import staff_roles from helpers.members import get_division_members from helpers.config import VATSIM_MEMBER_ROLE, CHECK_MEMBERS_INTERVAL, VATSCA_MEMBER_ROLE, ROLE_REASONS, GUILD_ID, DEBUG @@ -89,7 +89,7 @@ async def check_members_loop(self): guild_ids = [GUILD_ID] @cog_ext.cog_slash(name="checkusers", guild_ids=guild_ids, description="Refresh roles based on division membership.") - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def user_check(self, ctx: SlashContext): await ctx.send("Member refresh in progress") await self.check_members(True) diff --git a/cogs/update_messages.py b/cogs/update_messages.py index 9261f7f..cac221a 100644 --- a/cogs/update_messages.py +++ b/cogs/update_messages.py @@ -5,7 +5,7 @@ from helpers.config import ROLES_CHANNEL, GUILD_ID, RULES_CHANNEL, DIVISION_URL from helpers.message import embed -from helpers.message import roles +from helpers.message import staff_roles class UpdateCountryMessage(commands.Cog): @@ -45,7 +45,7 @@ def __init__(self, bot): ) ]) - @commands.has_any_role(*roles()) + @commands.has_any_role(*staff_roles()) async def update(self, ctx, *, message_id: str = None, option: str): """ Function posts updated countries message diff --git a/helpers/config.py b/helpers/config.py index 4f25228..7e1a71f 100644 --- a/helpers/config.py +++ b/helpers/config.py @@ -5,10 +5,6 @@ load_dotenv('.env') -PREFIXES = [ - '!', '.', '?', '#' -] - DESCRIPTION = 'This is a new VATSCA Discord Bot' PRESENCE_TEXT = 'VATSCA Airspace' @@ -17,7 +13,6 @@ COGS = [ 'cogs.admin', 'cogs.member', - #'cogs.announcement', 'cogs.tasks', 'cogs.events', 'cogs.update_messages', @@ -27,19 +22,12 @@ COGS_LOAD = { 'admin': 'cogs.admin', 'member': 'cogs.member', - #'announcement': 'cogs.announcement', 'check_members': 'cogs.tasks', 'events': 'cogs.events', 'update': 'cogs.update_messages', 'staffing': 'cogs.staffing' } -ADMIN_ROLES = [ - 'Web', - 'Discord Moderator', - 'Discord Administrator', -] - STAFF_ROLES = [ 'Web', 'Discord Moderator', @@ -101,10 +89,6 @@ GET_EVENTS_INTERVAL = int(os.getenv('GET_EVENTS_INTERVAL', 900)) DELETE_EVENTS_INTERVAL = int(os.getenv('DELETE_EVENTS_INTERVAL')) -def prefix() -> list: - return PREFIXES - - def activity() -> discord.Activity: return discord.Activity(type=discord.ActivityType.watching, name=PRESENCE_TEXT) diff --git a/helpers/event.py b/helpers/event.py index 527adba..d9393f5 100644 --- a/helpers/event.py +++ b/helpers/event.py @@ -2,7 +2,7 @@ import os from datetime import datetime, timedelta -from helpers.message import embed, event_description, get_image +from helpers.message import event_description, get_image from helpers.config import EVENT_CALENDAR_URL, FORUM_API_TOKEN class Event(): diff --git a/helpers/message.py b/helpers/message.py index a303cc2..84233ef 100644 --- a/helpers/message.py +++ b/helpers/message.py @@ -2,7 +2,7 @@ import discord from markdownify import markdownify as md -from helpers.config import ADMIN_ROLES, STAFF_ROLES, VATSCA_BLUE +from helpers.config import STAFF_ROLES, VATSCA_BLUE def embed(description: str = None, colour=None, title: str = None, author: dict = None, url: str = None, image: str = None, footer: dict = None, @@ -51,15 +51,6 @@ def embed(description: str = None, colour=None, title: str = None, author: dict return embed - -def roles() -> str: - """ - Function returns tuple of admin roles - :return: - """ - admin_roles = tuple(ADMIN_ROLES) - return admin_roles - def staff_roles() -> str: """ Function returns tuple of staff roles