Skip to content

Commit

Permalink
Change to CustomActivity and fix reconnect issue using a task
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulMarisOUMary committed Aug 11, 2023
1 parent 2056f88 commit 5713ea8
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions cogs/status.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import asyncio
import discord

from discord.ext import commands
from discord.ext import commands, tasks

from classes.discordbot import DiscordBot

class Status(commands.Cog, name="status"):
"""A loop to set the current status of the bot."""
def __init__(self, bot: DiscordBot) -> None:
self.bot = bot
self.subconfig_data = self.bot.config["cogs"][self.__cog_name__.lower()]

self.count = 0

"""def help_custom(self) -> tuple[str]:
emoji = '🏷️'
Expand All @@ -17,23 +19,25 @@ def __init__(self, bot: DiscordBot) -> None:
return emoji, label, description"""

async def cog_load(self) -> None:
self.task_change_status = self.bot.loop.create_task(self.loop_change_status())
self.task_change_status.change_interval(seconds=self.subconfig_data["cooldown"])
self.task_change_status.start()

async def cog_unload(self) -> None:
self.task_change_status.cancel()

async def loop_change_status(self) -> None:
@tasks.loop()
async def task_change_status(self) -> None:
await self.bot.wait_until_ready()
subconfig_data = self.bot.config["cogs"][self.__cog_name__.lower()]
while not self.bot.is_closed():
for status in subconfig_data["status"]:
await self.bot.change_presence(
activity=discord.Streaming(
name=status,
url="https://www.twitch.tv/warriormachine_"),
status=discord.Status.do_not_disturb
)
await asyncio.sleep(subconfig_data["cooldown"])

await self.bot.change_presence(
activity=discord.CustomActivity(
name=self.subconfig_data["status"][self.count],
emoji=None # Not supported (yet) by discord
),
status=discord.Status.do_not_disturb
)

self.count = (self.count + 1) % len(self.subconfig_data["status"])



Expand Down

0 comments on commit 5713ea8

Please sign in to comment.