utilise redis ping

This commit is contained in:
Nexus 2024-05-05 01:12:16 +01:00
parent cd45dc4844
commit 87510bd73a
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import io import io
import logging
import textwrap import textwrap
import redis import redis
import json import json
@ -13,6 +14,7 @@ from discord.ext import commands
class StarBoardCog(commands.Cog): class StarBoardCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.log = logging.getLogger("jimmy.starboard")
self.lock = asyncio.Lock() self.lock = asyncio.Lock()
self.redis = redis.asyncio.Redis(decode_responses=True) self.redis = redis.asyncio.Redis(decode_responses=True)
@ -48,6 +50,9 @@ class StarBoardCog(commands.Cog):
embeds = [embed, *starboard_message.embeds[1:]] embeds = [embed, *starboard_message.embeds[1:]]
await starboard_message.edit(embeds=embeds, file=discord.File(file, filename=filename)) await starboard_message.edit(embeds=embeds, file=discord.File(file, filename=filename))
async def _ping_check(self):
return await self.redis.ping()
async def generate_starboard_embed(self, message: discord.Message) -> discord.Embed: async def generate_starboard_embed(self, message: discord.Message) -> discord.Embed:
star_count = [x for x in message.reactions if str(x.emoji) == "\N{white medium star}"] star_count = [x for x in message.reactions if str(x.emoji) == "\N{white medium star}"]
if not star_count: if not star_count:
@ -126,6 +131,9 @@ class StarBoardCog(commands.Cog):
async def on_star_add(self, payload: discord.RawReactionActionEvent): async def on_star_add(self, payload: discord.RawReactionActionEvent):
if not payload.guild_id: if not payload.guild_id:
return return
if not await self._ping_check():
self.log.warning("Redis ping check failed - redis offline?")
return
async with self.lock: async with self.lock:
if str(payload.emoji) != "\N{white medium star}": if str(payload.emoji) != "\N{white medium star}":
return return
@ -219,7 +227,11 @@ class StarBoardCog(commands.Cog):
@commands.message_command(name="Starboard Info") @commands.message_command(name="Starboard Info")
@discord.guild_only() @discord.guild_only()
async def get_starboard_info(self, ctx: discord.ApplicationContext, message: discord.Message): async def get_starboard_info(self, ctx: discord.ApplicationContext, message: discord.Message):
data = await self.redis.get(str(message.id)) if not await self._ping_check():
self.log.warning("Redis ping check failed - redis offline?")
data = {"warning": "redis offline - ping failed."}
else:
data = await self.redis.get(str(message.id))
return await ctx.respond( return await ctx.respond(
'```json\n%s\n```' % json.dumps(data, indent=4), '```json\n%s\n```' % json.dumps(data, indent=4),
embed=await self.generate_starboard_embed(message) embed=await self.generate_starboard_embed(message)