@@ -187,50 +187,55 @@ async def send_news_ping(self, message):
187187 f"*{ t } *" for t in titles
188188 )
189189
190- channelinfo = await self .bot .db .fetchrow (
191- "SELECT * FROM newschannelsping WHERE channel_id = $1" , channel .id
192- )
193- if channelinfo :
194- if channelinfo ["latest_message_id" ]:
195- try :
196- oldmsg = await channel .fetch_message (
197- channelinfo ["latest_message_id" ]
198- )
199- except NotFound :
200- pass
190+ key = f"jocasta:bot:newschannelsping:{ channel .id } "
191+ latest_message_id = None
192+ if self .bot .redis :
193+ try :
194+ latest_message_id = await self .bot .redis .get (key )
195+ except Exception as e :
196+ print (f"[News] Failed to read from Redis: { e } " )
197+
198+ if latest_message_id :
199+ try :
200+ oldmsg = await channel .fetch_message (int (latest_message_id ))
201+ except NotFound :
202+ pass
203+ except ValueError :
204+ print (
205+ f"[News] Invalid message_id in Redis for channel "
206+ f"{ channel .id } : { latest_message_id !r} "
207+ )
208+ except Exception as e :
209+ print (f"[News] Failed to fetch old message: { e } " )
210+ else :
211+ if (
212+ discord .utils .utcnow () - oldmsg .created_at
213+ ).total_seconds () >= newspingbuffertime :
214+ await oldmsg .delete ()
201215 else :
202- if (
203- discord .utils .utcnow () - oldmsg .created_at
204- ).total_seconds () >= newspingbuffertime :
205- await oldmsg .delete ()
206- else :
207- current = [
208- i .strip ("*" )
209- for i in oldmsg .content .replace (
210- f"{ self .newsrole .mention } " , ""
211- ).split ("\n " )
212- ]
213- for t in titles :
214- if t not in current :
215- current .append (t )
216- new = formatmsg (self .newsrole .mention , current )
217- await oldmsg .edit (content = new )
218- return
219- else :
220- await self .bot .db .execute (
221- "INSERT INTO newschannelsping (channel_id) VALUES ($1)" , channel .id
222- )
216+ current = [
217+ i .strip ("*" )
218+ for i in oldmsg .content .replace (
219+ f"{ self .newsrole .mention } " , ""
220+ ).split ("\n " )
221+ ]
222+ for t in titles :
223+ if t not in current :
224+ current .append (t )
225+ new = formatmsg (self .newsrole .mention , current )
226+ await oldmsg .edit (content = new )
227+ return
223228
224229 msg = await channel .send (
225230 formatmsg (self .newsrole .mention , titles ),
226231 view = self .PingRoleView (self .newsrole , "Add/Remove Ping Role" ),
227232 )
228233
229- await self .bot .db . execute (
230- "UPDATE newschannelsping SET latest_message_id = $2 WHERE channel_id = $1" ,
231- channel . id ,
232- msg . id ,
233- )
234+ if self .bot .redis :
235+ try :
236+ await self . bot . redis . set ( key , str ( msg . id ))
237+ except Exception as e :
238+ print ( f"[News] Failed to write to Redis: { e } " )
234239
235240 @commands .Cog .listener ()
236241 async def on_member_join (self , member ):
0 commit comments