简体   繁体   English

如何停止运行discord bot进程(python)

[英]How to stop running discord bot process (python)

Im new at this discord.py thing.我是这个 discord.py 的新手。 I've just done a discord.py bot, it works ok, but sometimes the bot repeats constantly the command messages.我刚刚做了一个 discord.py 机器人,它工作正常,但有时机器人会不断重复命令消息。 I googled this problem and found out that maybe is for running the script over and over again (like when you save and run after edited or added functions).我在谷歌上搜索了这个问题,发现可能是为了一遍又一遍地运行脚本(比如在编辑或添加功能后保存和运行时)。 So I want to stop running the process, just like when I restart windows, the bot is offline (if I run the script after restarting windows, the bot acts normaly).所以我想停止运行这个进程,就像我重新启动windows时,bot离线(如果我在重新启动windows后运行脚本,bot正常运行)。 Pls help请帮忙

If someone needs the code, I can paste it here then.如果有人需要代码,我可以将其粘贴到此处。

PD: I made the bot exact as a tutorial... PD:我把机器人做成了一个教程......

This message really is not discord.py specific and applies to every script which runs indefinetely.此消息确实不是 discord.py 特定的,并且适用于不确定运行的每个脚本。

You are running multiple instances of your bot.您正在运行机器人的多个实例。 If you run it in an IDE envoirment, then there should be a stop button somewhere.如果您在 IDE 环境中运行它,那么某处应该有一个停止按钮。 If you are running it in console, closing the console window will close the bot.如果您在控制台中运行它,关闭控制台窗口将关闭机器人。

Edit: If you are running it in sublime3 like your tags suggest, every time you want to close your bot, go to "Tools" and then "Cancel Build" (hotkey: CTRL + Break).编辑:如果您像标签建议的那样在 sublime3 中运行它,每次要关闭机器人时,请转到“工具”,然后转到“取消构建”(热键:CTRL + Break)。 As soon as you run another instance of your bot, sublime "decouples" the current script in favour of the new one and this method does not work anymore.一旦您运行机器人的另一个实例,sublime 就会“解耦”当前脚本以支持新脚本,并且此方法不再起作用。 Then you have to manually go through your running processes (command line or Task Manager) and search for any "Python" processes.然后您必须手动检查正在运行的进程(命令行或任务管理器)并搜索任何“Python”进程。

In general I reccomend running the script in the commandline instead as you have more control over it.一般来说,我建议在命令行中运行脚本,因为您可以更好地控制它。

  • If you add the code that I wrote down there (that only the owner can use) will shut down the already running bots (write /shutdown in discord server or whatever your prefix is).如果你添加我在那里写的代码(只有所有者可以使用)将关闭已经运行的机器人(在不和谐服务器中写入/shutdown或任何你的前缀)。

However, you may need a PC restart after saving the bot with this code.但是,使用此代码保存机器人后,您可能需要重新启动 PC。

@client.command()
@commands.is_owner()
async def shutdown(ctx):
    await ctx.bot.logout()
  • So every time if you want to edit your command, you write /shutdown and edit it, after that, you can start it again.所以每次如果你想编辑你的命令,你写 /shutdown 并编辑它,之后,你可以再次启动它。

I hope this works for you and that I could help.我希望这对您有用,并且我可以提供帮助。

A way to end the entire script your bot is running on is by using the built in python functions exit() and quit() both do the same thing.结束机器人正在运行的整个脚本的一种方法是使用内置的 Python 函数exit()quit()都做同样的事情。

@bot.command()
@commands.is_owner()
async def shutdown(context):
    exit()

by putting @commands.is_owner() you're making it so that only the owner of the bot can use this command.通过放置@commands.is_owner()您正在制作它,以便只有机器人的所有者才能使用此命令。 To invoke this command type /shutdown in your discord server (replace the / with whatever your prefix is).要调用此命令,请在您的 Discord 服务器中输入/shutdown (将/替换为您的任何前缀)。

It raises a bunch of errors but overall ends the program so conclusively it may not be the most efficient method but it does the job.它引发了一堆错误,但总体上结束了程序,因此最终它可能不是最有效的方法,但它可以完成工作。

Easy way to solve this is by regenerating your bot token.解决这个问题的简单方法是重新生成您的机器人令牌。 Doing so will shutdown all active scripts.这样做将关闭所有活动脚本。 For anyone new to the Discord API, if you ever get an error that says something on the lines of 'Access Denied,' this solution should also help you.对于不熟悉 Discord API 的任何人,如果您收到“拒绝访问”这样的错误消息,此解决方案也应该对您有所帮助。

just type exit in visual studio code output terminal and your bot will be disconnected.只需在 Visual Studio 代码输出终端中输入exit ,您的机器人就会断开连接。 Enjoy the day享受这一天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM