[otherbot] Avoid a lot of config calls. (#131)

* [otherbot] Avoid a lot of config calls.

* after.guild
This commit is contained in:
Jamie
2020-07-01 00:24:59 +01:00
committed by GitHub
parent 9ae4bf7dc5
commit 402a4728d6
2 changed files with 14 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
from .otherbot import Otherbot
def setup(bot):
bot.add_cog(Otherbot(bot))
async def setup(bot):
cog = Otherbot(bot)
await cog.generate_cache()
bot.add_cog(cog)

View File

@@ -11,6 +11,9 @@ class Otherbot(commands.Cog):
self.config.register_guild(**default_guild)
async def generate_cache(self):
self.otherbot_cache = await self.config.all_guilds()
@commands.group()
@commands.guild_only()
@checks.admin_or_permissions(manage_roles=True)
@@ -29,6 +32,7 @@ class Otherbot(commands.Cog):
channel = ctx.channel
await self.config.guild(ctx.guild).reporting.set(channel.id)
await ctx.send(f"Reporting channel set to: {channel.mention}.")
await self.generate_cache()
@otherbot.command()
async def pingrole(self, ctx, role_name: discord.Role = None):
@@ -40,6 +44,7 @@ class Otherbot(commands.Cog):
pingrole_id = await self.config.guild(ctx.guild).ping()
pingrole_obj = discord.utils.get(ctx.guild.roles, id=pingrole_id)
await ctx.send(f"Ping role set to: {pingrole_obj.name}.")
await self.generate_cache()
@otherbot.command()
@checks.admin_or_permissions(manage_roles=True)
@@ -48,6 +53,7 @@ class Otherbot(commands.Cog):
async with self.config.guild(ctx.guild).watching() as watch_list:
watch_list.remove(bot_user.id)
await ctx.send(f"Not watching {bot_user.mention} any more.")
await self.generate_cache()
@otherbot.command()
@checks.admin_or_permissions(manage_roles=True)
@@ -77,12 +83,15 @@ class Otherbot(commands.Cog):
await ctx.send(
f"Reporting channel set to: {ctx.message.channel.mention}. Use `{ctx.prefix}otherbot channel` to change this."
)
await self.generate_cache()
@commands.Cog.listener()
async def on_member_update(self, before, after):
if after.guild is None or not after.bot:
return
data = await self.config.guild(after.guild).all()
data = self.otherbot_cache.get(after.guild.id)
if data is None:
return
if after.status == discord.Status.offline and (after.id in data["watching"]):
channel_object = self.bot.get_channel(data["reporting"])
if not data["ping"]: