关闭Discord bot连接而不终止命令行(Discord.py)



目的:添加更多命令而不中断其他命令

我一直在四处寻找一种在不中断我的机器人程序流程的情况下推出更新的方法,因为它有一些异步函数,在该函数被称为后会执行一段时间

我尝试过:等待客户端.logout((

上面会注销机器人程序,但也会关闭命令行。我在不和谐的.py文档上找到了这个。

我运行的是Windows 10,Python版本3.9

如有任何帮助,我们将不胜感激。提前谢谢。

如果您不想终止当前进程,而只想热重新加载不同的功能,您可能需要研究discord.py的扩展功能。使用扩展和齿轮将允许您在不停止机器人的情况下启用/禁用/重新加载机器人中的某些功能(这应该会保持任务的运行(。它也是热重新加载的内置方法。

扩展和cog通常一起使用(尽管它们不一定要一起使用(。可以为要一起重新加载的每组类似命令创建文件。

以下代码示例应集成到您的设置中。您可能还想添加错误处理和输入检查,但它应该让您了解发生了什么。有关详细解释或方法选项,请查看文档。

# info.py
# ...
# this is just an example
class Info(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def about(self, ctx):
await ctx.send("something here")
# ...
# extensions need a setup function
def setup(bot):
bot.add_cog(Info(bot))
# main.py
# ...
bot = commands.Bot(
# ...
)
bot.load_extension("info")
bot.run(token)
# reload command
@bot.command()
async def reload(ctx, extension):
# probably want to add a check to make sure
# only you can reload things.
# also handle the input
bot.reload_extension(extension)
to use, you might do something like `prefix!reload info`

例如,在同一目录中创建一个名为startup.py的新python文件。在此文件中执行以下操作:

import os
import time
time.sleep(5)
os.system('python YOUR_BOTS_FILE_NAME.py')

然后在你的机器人代码所在的文件中添加一个新命令,我们将称之为重启,例如:

import discord
from discord.ext import commands
import os
@client.command()
@commands.is_owner()
async def restart(ctx):
os.system('python startup.py')
exit()

在startup.py文件中,操作系统会等待5秒钟,等待机器人程序的文件关闭,然后再打开。机器人程序文件中的restart命令会启动启动文件,然后自行关闭。

@commands.is_owner((

确保消息的作者是你,这样人们就不会重新启动你的机器人。

我自己正在开发一个机器人,我自己制作了一个shutdown命令,该命令在不使用终端的情况下关闭机器人。

首先我会添加代码,然后解释它。

代码:

myid = <my discord account ID>
@MyBot.command()
async def shutdown(ctx):
if ctx.author.id == myid:
shutdown_embed = discord.Embed(title='Bot Update', description='I am now shutting down. See you later. BYE! :slight_smile:', color=0x8ee6dd)
await ctx.channel.send(embed=shutdown_embed)
await MyBot.logout()
if ctx.author.id != myid:
errorperm_embed = discord.Embed(title='Access Denied!', description='This command is `OWNER` only. You are not allowed to use this. Try not to execute it another time.', color=0xFF0000)
errorperm_embed.set_footer(text=ctx.author)
await ctx.channel.send(embed=errorperm_embed, delete_after=10.0)

我没有添加任何has_permissions,因为当我使用discord ID来限制其使用时,我不需要它。

说明:

  • 我定义了一个变量myid,它等于我的discord帐户ID。请在此处查看如何获取用户ID:

https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-

  • 我添加了一个条件,即使用此命令的用户的ID是否等于myid。如果它等于我的帐户ID,那么它会关闭机器人,否则它会向用户显示错误。

  • 我只使用了await MyBot.logout(),它会将您注销并断开连接。

您可以将代码放入while True循环中。

while True:
client = commands.Bot(command_prefix='!')
async def restart(ctx):
await client.logout()
client.run('token')

您可以用相同的流程替换当前流程,重新开始。您必须事先刷新缓冲区并关闭文件指针,但这很容易做到:

import os
import sys
from typing import List
def restart(filepointers: List):
# this cleanup part is optional, don't need it if your bot is ephemeral
# flush output buffers
sys.stdout.flush()
sys.stderr.flush()
# flush and close filepointers
for fp in filepointers:
os.fsync(fp)
fp.close()
# replace current process
os.execl(*sys.argv)

然后用你的机器人像你一样(从同一个文件(调用这个函数。

如果要更新代码,必须重新启动程序。

import os
path = "your .py file path"
@client.command(name="restart")
async def restart_command(ctx):
await client.close()
os.system("python " + path)

最新更新