0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Actually, what the two commands do is add/remove roles to all members of the guild so that they can't access the channels anymore. I think that's why they look so alike. --EDIT-- So you might notice that close-server command ends with: await user.remove_roles(bot_offline)... While open-server ends with await user.remove_roles(bot_offline)...
But yeah what you told me to do could be a more simplified version of my code. I'll revise that, thanks.
I saw that after I posted what I said. apologies on my end. If you're able to get your version working, then please let me know. I would love to see the end result!
Don't worry! Also, later on, you can delete your answer if you see something is wrong. And if you want to change it, you can also edit it.
ty ty kind sir/madam :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.