Python 3.x 在bot脱机之前运行命令
我目前在Heroku上托管discord机器人,它一直运行良好。但Heroku的一个问题是,我只有大约500小时的免费托管时间(我不记得确切的数字)。这对我来说有点烦人,因为我使用机器人来运行建议频道。当机器人离线时,无论何时有人在建议频道中说了什么,机器人都不会做出响应(机器人应该删除用户的消息并在嵌入中再次发送) 我找到的解决方案是创建一个命令,在bot即将脱机时关闭建议通道。我也这样做了很长一段时间,直到有一天,我忘了关掉它。然后,每个人都在不知道发生了什么的情况下通过建议频道发送消息。现在,我想知道是否有办法让bot在离线前运行通道关闭命令。以下是关闭通道的命令:Python 3.x 在bot脱机之前运行命令,python-3.x,discord.py,Python 3.x,Discord.py,我目前在Heroku上托管discord机器人,它一直运行良好。但Heroku的一个问题是,我只有大约500小时的免费托管时间(我不记得确切的数字)。这对我来说有点烦人,因为我使用机器人来运行建议频道。当机器人离线时,无论何时有人在建议频道中说了什么,机器人都不会做出响应(机器人应该删除用户的消息并在嵌入中再次发送) 我找到的解决方案是创建一个命令,在bot即将脱机时关闭建议通道。我也这样做了很长一段时间,直到有一天,我忘了关掉它。然后,每个人都在不知道发生了什么的情况下通过建议频道发送消息。现
@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`___**')
@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`___**')
这是重新打开通道的命令:
@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`___**')
@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`___**')
使用当前脚本关闭和打开服务器,我应该怎么做?我建议将您的命令重新格式化为以下格式:
@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
您可以找到有关更改频道权限的文档
但这正是我所建议的,沿着这条路线做一些事情。抱歉,如果这不是你的目标,但我认为这可能有助于优化,等等。当您将命令写入
open
频道备份时,将命令名从close
更改为open
,然后在overwrites={}
中切换您的权限,如果您选择这样做的话,这将完成您的任务。实际上,这两个命令的作用是向公会的所有成员添加/删除角色,这样他们就不能再访问频道了。我想这就是他们看起来如此相似的原因--编辑--所以您可能会注意到,close server
命令以:wait user.remove_角色(bot_脱机)
结尾。。。而打开服务器
以等待用户结束。删除角色(bot\u离线)
…但是是的,你让我做的可能是我代码的更简化版本。我会修改的,谢谢。我在发布我说的内容后看到了。我向你道歉。如果你能让你的版本工作,那么请让我知道。我很想看到最终的结果!别担心!此外,稍后,如果您发现问题,您可以删除您的答案。如果您想更改它,也可以对其进行编辑。ty kind先生/女士:)