mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 02:16:09 -05:00
[V3 Admin] Rewrite of Squid-Plugins Admin cog (#825)
* Add/remove roles * Announcement * Edit role stuff A bit of refactoring * Selfrole stuff * Add announce ignore capabilities * announce configurations Announcement fixes * Serverlock initial commit * Add some admin tests better test * Update for new config * Add user hierarchy checks * Fix tests * Update from rebase * Fix config getter * Fix async getters/selfrole
This commit is contained in:
71
redbot/cogs/admin/announcer.py
Normal file
71
redbot/cogs/admin/announcer.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import asyncio
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
class Announcer:
|
||||
def __init__(self, ctx: commands.Context,
|
||||
message: str,
|
||||
config=None):
|
||||
"""
|
||||
:param ctx:
|
||||
:param message:
|
||||
:param config: Used to determine channel overrides
|
||||
"""
|
||||
self.ctx = ctx
|
||||
self.message = message
|
||||
self.config = config
|
||||
|
||||
self.active = None
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
Starts an announcement.
|
||||
:return:
|
||||
"""
|
||||
if self.active is None:
|
||||
self.active = True
|
||||
self.ctx.bot.loop.create_task(self.announcer())
|
||||
|
||||
def cancel(self):
|
||||
"""
|
||||
Cancels a running announcement.
|
||||
:return:
|
||||
"""
|
||||
self.active = False
|
||||
|
||||
async def _get_announce_channel(self, guild: discord.Guild) -> 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.default_channel
|
||||
|
||||
return channel
|
||||
|
||||
async def announcer(self):
|
||||
guild_list = self.ctx.bot.guilds
|
||||
bot_owner = (await self.ctx.bot.application_info()).owner
|
||||
for g in guild_list:
|
||||
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:
|
||||
await bot_owner.send("I could not announce to guild: {}".format(
|
||||
g.id
|
||||
))
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
self.active = False
|
||||
|
||||
Reference in New Issue
Block a user