[Announcer] Don't die with a lack of channels (#4089)

This commit is contained in:
Michael H
2020-07-20 13:51:27 -04:00
committed by GitHub
parent 5507816c42
commit ec262d4c30
2 changed files with 52 additions and 40 deletions

View File

@@ -1,4 +1,5 @@
import asyncio
from typing import Optional
import discord
from redbot.core import commands
@@ -38,20 +39,9 @@ class Announcer:
"""
self.active = False
async def _get_announce_channel(self, guild: discord.Guild) -> discord.TextChannel:
async def _get_announce_channel(self, guild: discord.Guild) -> Optional[discord.TextChannel]:
channel_id = await self.config.guild(guild).announce_channel()
channel = None
if channel_id is not None:
channel = guild.get_channel(channel_id)
if channel is None:
channel = guild.system_channel
if channel is None:
channel = guild.text_channels[0]
return channel
return guild.get_channel(channel_id)
async def announcer(self):
guild_list = self.ctx.bot.guilds
@@ -60,15 +50,16 @@ class Announcer:
if not self.active:
return
if await self.config.guild(g).announce_ignore():
continue
channel = await self._get_announce_channel(g)
try:
await channel.send(self.message)
except discord.Forbidden:
failed.append(str(g.id))
if channel:
if channel.permissions_for(g.me).send_messages:
try:
await channel.send(self.message)
except discord.Forbidden:
failed.append(str(g.id))
else:
failed.append(str(g.id))
if failed:
msg = (