繁体   English   中英

Python:为什么我的 discord 机器人会为单个命令打印多个输出? 此外,我如何重新启动/关闭我的机器人?

[英]Python: Why does my discord bot print multiple outputs for a single command? Additionally, how do I restart/shut down my bot?

我对 python 真的很陌生,而且在一般编码方面相对缺乏经验,所以这是我自己无法真正解决的问题。

机器人在我尝试运行命令的前几次没有复制输出,但是在我添加更多命令后它开始增加输出。 我还剪切并粘贴了一堆代码行,但在我的文本编辑器(Atom)中没有任何错误。

另外,如何轻松重启我的机器人? 我正在使用Atom,我没有看到明显的方法......

这是我使用的代码(最后剩下的就是令牌):

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():

       print("Hello world")

@client.event
async def on_member_join(member):
    print(f'{member} has joined a server')

@client.event
async def on_member_remove(member):
    print(f'{member} has left a server')


@client.command()
async def moon(ctx):
    await ctx.send('dancer')

@client.command()
async def ping(ctx):
    await ctx.send(f'{round(client.latency * 1000)}ms')


@client.command()
async def test(ctx):
    await ctx.send('test')

例子

另一个例子

我感谢大家的所有意见,谢谢!

机器人乘以 output 很可能是一个错误,因为我看不出你做错了什么。 I think it may be a problem with your IDE - I don't recommend Atom to run Python scripts you should use VScode or PyCharm - or a problem with your Python interpreter.

为了轻松关闭机器人,我在机器人中使用了以下命令:

import sys
import discord
from discord.ext import commands

client = commands.Bot(command_prefix = ".")

@client.command()
async def quit(ctx):
    sys.exit()

client.run("")

之后,只需再次运行您的程序。 但最简单的方法是手动终止您的程序。

我能想到的重启机器人的最快方法是简单地手动停止 python 代码。 我在 atom 中使用atom-python-run package 来运行我的 python 代码。 我只需按 F5 即可启动代码,它会在 atom 文本编辑器之外打开控制台。 从那里停止我的代码就像关闭 window 一样简单。

暂无
暂无

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

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