diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index 8729bef..f715df9 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -2,9 +2,6 @@ Defines cog for guild listeners. Contains functions for various guild events. """ -import os -import json - from nextcord.ext import commands import nextcord @@ -20,7 +17,6 @@ class GuildListener(commands.Cog, name="Guild Listener"): @commands.Cog.listener() async def on_guild_join(self, guild: nextcord.Guild): """Listener for Guild Join. Creates skeleton for new guild joined and saves it.""" - # turning guild id into string because json doesn't support ints as object keys. highscores_data.add_new_server(guild.id) welcome_message: str = "Hello, I keep track of Highscores for you!" diff --git a/cogs/presence/__init__.py b/cogs/presence/__init__.py new file mode 100644 index 0000000..ac93294 --- /dev/null +++ b/cogs/presence/__init__.py @@ -0,0 +1,54 @@ +""" +Functions for controlling the bot's presence. +""" + +import random + +from nextcord.ext import commands +from nextcord.ext import tasks +import nextcord + + +class BotPresence(commands.Cog, name="Bot Presence"): + """Class for bot presence functions.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_guild_join(self): + """Listener for Guild Join. Updated presence on guild join.""" + await self.change_presence() + + @commands.Cog.listener() + async def on_ready(self): + """Starts the loop when bot is ready.""" + await self.bot.wait_until_ready() + # pylint doesn't recognize that periodic_change_presense is subclassed from tasks.loop. + self.periodic_change_presence.start() # pylint: disable=no-member + + @tasks.loop(minutes=2) + async def periodic_change_presence(self): + """Loops every 2 minutes and changes presence.""" + await self.change_presence() + + async def change_presence(self): + """Randomly chooses a presence and updates bot's presence.""" + statuses = [ + "?help", + f"{len(self.bot.guilds)} servers", + "Vuln Pls.", + "All your base are belong to us.", + "Double plus good.", + ] + + status = random.choice(statuses) + + await self.bot.change_presence(activity=nextcord.Game(status)) + + +# This function will be called when this extension is loaded. +# It is necessary to add these functions to the bot. +def setup(bot: commands.Bot): + """Adds function to bot""" + bot.add_cog(BotPresence(bot)) diff --git a/highscores.py b/highscores.py index 6cc103a..681e59a 100644 --- a/highscores.py +++ b/highscores.py @@ -45,15 +45,9 @@ def main(): # To enable the members priveleged intent: # intents.members = True - # Set custom status to "Listening to ?help" - activity = nextcord.Activity( - type=nextcord.ActivityType.listening, name=f"{config.BOT_PREFIX}help" - ) - bot = commands.Bot( commands.when_mentioned_or(config.BOT_PREFIX), intents=intents, - activity=activity, ) # Get the modules of all cogs whose directory structure is ./cogs/