'DiscordPy Bot abnormally long shutdown times
So here's the crux of the problem. I'm using the Discord Py API (1.7.3). I have 2 bots I run that use it, Red Bot and the one I made myself. I've come to notice one little discrepancy that I can't troubleshoot successfully. If I kill Red Bot, like either by shutting down the host computer or otherwise, it leaves Chat immediately. If I do the same to my bot, it lingers for minutes at a time. It's driving me up the wall and I can't figure out why. I have another friend with his own completely independent bot, with the same issue.
There are 5 other modules, but the below is main and the most likely to have either the problem or the place for the solution.
from events import Events
from macros import Macros
from voting import Voting
from miscellaneous import Miscellaneous
from persistent import *
# Fetches configuration information
comList = []
discordAuthcode = ""
with open(DAC_FILE, "r") as discordAuthfile:
discordAuthcode = discordAuthfile.read().strip()
# Sets Google's credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = GAJ_FILE
# Creates basic bot object
help_overwrite = commands.DefaultHelpCommand(no_category="Help")
intents_overwrite = discord.Intents.default()
intents_overwrite.members = True
intents_overwrite.messages = True
bot = commands.Bot(command_prefix=COM_PRFX, description="Gestalt's Help Menu", help_command=help_overwrite, intents=intents_overwrite)
# Creates a task to iterate randomly through creative messages
@tasks.loop(minutes=random.randint(1,5))
async def random_presence():
choice = presenceList[random.randint(0, len(presenceList)-1)]
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=choice))
# Creates a task to backup active nomination instances
@tasks.loop(minutes=5)
async def backup_nominations():
repickles(NBP_FILE, nominationMap)
# DEBUG: Executes on Interval, good for live-testing
#@tasks.loop(seconds=3)
#async def backup_nominations():
# print(nominationMap)
# Bot Initialization
@bot.event
async def on_ready():
"""Initializes the bot's functions.
"""
# Feedback
print("{0.user} Version {1} has started.".format(bot, VERSION))
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="the beginning."))
# Starts Random Presence
random_presence.start()
# Starts backing up nominations list
backup_nominations.start()
# Adds Cogs to Bot
bot.add_cog(Events(bot))
bot.add_cog(Macros(bot))
bot.add_cog(Voting(bot))
bot.add_cog(Miscellenous(bot))
# Runs Bot
bot.run(discordAuthcode)
I've looked at Red Bot's handling of shutdown, and while it clearly puts a lot more effort into it, I didn't successfully transplant anything that worked. Could I please have some help?
Edit: I have also tried enabling logging, nothing is erroring out, the last entry is always it exiting the Event Loop.
Edit 2: I have replicated this problem on a barebones bot
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|