mirror of
https://github.com/MrUnknownDE/redbot-assets.git
synced 2026-04-19 14:53:47 +02:00
fix redbot error
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import discord
|
import discord
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from redbot.core import app_commands, commands, Config
|
from discord import app_commands
|
||||||
from discord.ext import tasks
|
from redbot.core import commands, Config
|
||||||
|
|
||||||
log = logging.getLogger("red.bierbaron")
|
log = logging.getLogger("red.bierbaron")
|
||||||
|
|
||||||
@@ -15,10 +16,22 @@ class Bierbaron(commands.Cog):
|
|||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.config = Config.get_conf(self, identifier=9483726154, force_registration=True)
|
self.config = Config.get_conf(self, identifier=9483726154, force_registration=True)
|
||||||
self.config.register_guild(live_channel=None, live_message=None)
|
self.config.register_guild(live_channel=None, live_message=None)
|
||||||
self.live_update_task.start()
|
self._live_task: asyncio.Task | None = None
|
||||||
|
|
||||||
|
async def cog_load(self):
|
||||||
|
"""Called by Redbot after the cog is fully loaded."""
|
||||||
|
self._live_task = asyncio.create_task(self._live_loop())
|
||||||
|
|
||||||
def cog_unload(self):
|
def cog_unload(self):
|
||||||
self.live_update_task.cancel()
|
if self._live_task:
|
||||||
|
self._live_task.cancel()
|
||||||
|
|
||||||
|
async def _live_loop(self):
|
||||||
|
"""Background loop that updates all live embeds every 15 seconds."""
|
||||||
|
await self.bot.wait_until_red_ready()
|
||||||
|
while True:
|
||||||
|
await self._run_live_update()
|
||||||
|
await asyncio.sleep(15)
|
||||||
|
|
||||||
async def _fetch_api_data(self):
|
async def _fetch_api_data(self):
|
||||||
"""Fetches data from the Bierbaron API. Returns None on failure."""
|
"""Fetches data from the Bierbaron API. Returns None on failure."""
|
||||||
@@ -104,9 +117,8 @@ class Bierbaron(commands.Cog):
|
|||||||
|
|
||||||
# ── Background task ──────────────────────────────────────────────────────
|
# ── Background task ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
@tasks.loop(seconds=15)
|
async def _run_live_update(self):
|
||||||
async def live_update_task(self):
|
"""Fetches API data and updates all registered live embeds."""
|
||||||
"""Updates all registered live embeds every 15 seconds."""
|
|
||||||
data = await self._fetch_api_data()
|
data = await self._fetch_api_data()
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
@@ -131,19 +143,16 @@ class Bierbaron(commands.Cog):
|
|||||||
message = await channel.fetch_message(message_id)
|
message = await channel.fetch_message(message_id)
|
||||||
await message.edit(embed=embed)
|
await message.edit(embed=embed)
|
||||||
except discord.NotFound:
|
except discord.NotFound:
|
||||||
# Message deleted – clear config
|
|
||||||
await self.config.guild(guild).live_message.set(None)
|
await self.config.guild(guild).live_message.set(None)
|
||||||
await self.config.guild(guild).live_channel.set(None)
|
await self.config.guild(guild).live_channel.set(None)
|
||||||
log.info(f"Live embed message not found in guild {guild_id}, cleared config.")
|
log.info(f"Live embed message not found in guild {guild_id}, cleared config.")
|
||||||
except discord.Forbidden:
|
except discord.Forbidden:
|
||||||
log.warning(f"Missing permission to edit live embed in guild {guild_id}.")
|
log.warning(f"Missing permission to edit live embed in guild {guild_id}.")
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Error updating live embed in guild {guild_id}: {e}")
|
log.error(f"Error updating live embed in guild {guild_id}: {e}")
|
||||||
|
|
||||||
@live_update_task.before_loop
|
|
||||||
async def before_live_update_task(self):
|
|
||||||
await self.bot.wait_until_red_ready()
|
|
||||||
|
|
||||||
# ── Slash Commands ────────────────────────────────────────────────────────
|
# ── Slash Commands ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@app_commands.command(name="bierbaron")
|
@app_commands.command(name="bierbaron")
|
||||||
|
|||||||
Reference in New Issue
Block a user