GGberry
GGberry

Reputation: 945

Run command before bot goes offline

I'm currently a hosting discord bot on Heroku, and it has been working fine. But a problem with Heroku, is that I only have about 500 free hosting hours (I can't remember the exact number). That's slightly annoying for me, since I use the bot to run suggestion channels. And whenever someone says something in the suggestion channel while the bot is offline, there is no response from the bot (the bot is supposed to delete the user's message and send it again in embed).

The solution I found is to create a command to close the suggestion channel when the bot is about to go offline. I've been doing this as well for a long time, until one day, I forgot to turn it off. Then, everyone was sending messages in the suggestion channel without knowing what is happening. So now, I would like to know if there is a way to make the bot run the channel closing command before it goes offline. Here is the command for closing the channel:

@commands.command(aliases=['close-server'])
    @commands.is_owner()
    async def closeserver(self, ctx):
        if ctx.guild.id != 735105735792394251:
            return

        await ctx.channel.purge(limit=1)
        for user in ctx.guild.members:

            member = discord.utils.get(ctx.guild.roles, name='Member')

            bot_offline = discord.utils.get(ctx.guild.roles, name='Bot Currently Offline')

            if member in user.roles:
                await user.add_roles(bot_offline)
            else:
                not_verified = discord.utils.get(ctx.guild.roles, name='Not_Verified')
                await user.add_roles(not_verified)

            await user.remove_roles(member)
        announcements = self.bot.get_channel(735106648330469488)
        await announcements.send('**___`The server Is closed`___**')

And this is the command to re-open the channel:

@commands.command(aliases=['open-server'])
    @commands.is_owner()
    async def openserver(self, ctx):
        if ctx.guild.id != 735105735792394251:
            return

        await ctx.channel.purge(limit=1)
        for user in ctx.guild.members:

            member = discord.utils.get(ctx.guild.roles, name='Member')
            bot_offline = discord.utils.get(ctx.guild.roles, name='Bot Currently Offline')
            if bot_offline in user.roles:
                await user.add_roles(member)
            else:
                not_verified = discord.utils.get(ctx.guild.roles, name='Not_Verified')
                await user.remove_roles(not_verified)
            await user.remove_roles(bot_offline)
        announcements = self.bot.get_channel(735106648330469488)
        await announcements.send('**___`The server Is opened`___**')

Using my current script to close and open the server, what should I do?

Upvotes: 0

Views: 349

Answers (1)

shellbyy
shellbyy

Reputation: 73

I would recommend re-formatting your command into something like:

@commands.command()
@commands.is_owner()
async def close(self, ctx):

    member = ctx.message.author # set the person who wrote the message equal to a variable

    if ctx.guild.id != 735105735792394251: # your check system
        return
    await ctx.send("The Server Is Now Closed!") # announcement to be sent when command is executed
    
    overwrites = {
        overwrite.send_messages = False,
        overwrite.read_messages = True
    } # this overwrites the current permissions on the channel
    await channel.set_permissions(member, overwrites=overwrites) # typehint the member, and the overwrites

You can find the documentation for changing the channel permissions here but this is what I recommend doing something along the line of. Apologies if this isn't what you were aiming for, but I thought it might help both optimize, and such. And when you write your command to open the channel back up, change the command name from close to open, and then switch your permissions around in overwrites = {}, and that should accomplish what you're after if you so chose to go this route.

Upvotes: 1

Related Questions