Smarter chunking steps; don't chunk at startup
This commit is contained in:
parent
4ba94c106c
commit
4a45d5ce2b
29
bot.py
29
bot.py
|
@ -2,6 +2,7 @@ import discord
|
||||||
import logging
|
import logging
|
||||||
import pendulum
|
import pendulum
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import utils
|
import utils
|
||||||
|
@ -17,6 +18,7 @@ opts = {
|
||||||
"activity": discord.Activity(name=utils.default_status, type=0),
|
"activity": discord.Activity(name=utils.default_status, type=0),
|
||||||
"allowed_mentions": discord.AllowedMentions(everyone=False),
|
"allowed_mentions": discord.AllowedMentions(everyone=False),
|
||||||
"intents": intent,
|
"intents": intent,
|
||||||
|
"chunk_guilds_at_startup": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
bot = commands.AutoShardedBot(**opts)
|
bot = commands.AutoShardedBot(**opts)
|
||||||
|
@ -31,19 +33,21 @@ async def before_invocation(ctx):
|
||||||
except (discord.Forbidden, discord.HTTPException):
|
except (discord.Forbidden, discord.HTTPException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Ensure guild is chunked
|
# If this is a DM, or the guild has been chunked, we're done
|
||||||
if ctx.guild and not ctx.guild.chunked:
|
if not ctx.guild or ctx.guild.chunked:
|
||||||
await ctx.guild.chunk()
|
return
|
||||||
|
|
||||||
|
# Get a lock for the guild
|
||||||
@bot.event
|
lock = bot.chunked_guild_locks.get(ctx.guild.id)
|
||||||
async def on_ready():
|
# If one hasn't been created yet, create it
|
||||||
completion = "Ready in: ```\n{}```".format(
|
if lock is None:
|
||||||
(pendulum.now(tz="UTC") - bot.uptime).in_words()
|
lock = asyncio.Lock()
|
||||||
)
|
bot.chunked_guild_locks[ctx.guild.id] = lock
|
||||||
await bot.get_guild(214143647447253003).get_channel(214146604519784449).send(
|
# Now only try to chunk when the lock is available
|
||||||
completion
|
async with lock:
|
||||||
)
|
# Recheck if it's been chunked just in case, don't want to chunk if we don't have to
|
||||||
|
if ctx.guild and not ctx.guild.chunked:
|
||||||
|
await ctx.guild.chunk()
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
|
@ -138,4 +142,5 @@ if __name__ == "__main__":
|
||||||
bot.load_extension(e)
|
bot.load_extension(e)
|
||||||
|
|
||||||
bot.uptime = pendulum.now(tz="UTC")
|
bot.uptime = pendulum.now(tz="UTC")
|
||||||
|
bot.chunked_guild_locks = {}
|
||||||
bot.run(utils.bot_token)
|
bot.run(utils.bot_token)
|
||||||
|
|
Loading…
Reference in a new issue