【发布时间】:2019-11-17 10:16:54
【问题描述】:
我设置的 Discord 机器人起初运行良好,但现在在命令后重复消息(或发送多条消息)。我不确定如何解决这个问题。
我正在尝试为我担任员工的不和谐服务器设置机器人。员工团队知道我经常在网上查看机器人,并且我可以编写一些代码。他们问我是否可以为服务器制作一个机器人,所以我决定试一试。大约 1.5 小时后,我设置了一个机器人并给它一些命令来使用它。我尝试在网上搜索解决方案,但找不到任何适合该问题的解决方案。
这是我的“bot.py”文件中的主要代码。出于显而易见的原因,我在 client.run 中隐藏了 Bot 令牌。
import discord
import random
import os
from discord.ext import commands
client = commands.Bot(command_prefix = 'pj!')
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
client.run('[REDACTED]')
这是我在名为 Alpha.py(临时标题)的 Cog 中的代码。目前,只有 ping 和 help 命令有问题。
import discord
from discord.ext import commands
class Alpha(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print('Bot: Online')
@commands.command()
async def ping(self, ctx):
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
@commands.command()
async def _8ball(self, ctx, *, question):
responses = ['It is certain.',
'It is decidedly so.',
'Without a dobut.',
'Yes - definitely.',
'You may rely on it.',
'As I see it, yes.',
'Most likely.',
'Outlook good.',
'Yes.',
'Signs point to yes.',
'Reply hazy, try again.',
'Ask again later.',
'Better not tell you now.',
'Concentrate and ask again.',
"Don't count on it.",
'My reply is no.',
'My sources say no.',
'Outlook not so good.',
'Very doubtful.']
await ctx.send(f'Question: {question}\nAnswer: {random.choice(responses)}')
def setup(client):
client.add_cog(Alpha(client))
Ping Command 对于 ping 命令,我希望它会发布一条消息,“Pong![latency]ms”一次,其中包含 [latency] 所在的单个值。 相反,它会发布消息三次,其中 [latency] 是三个不同的值
Help Command 对于帮助命令,我希望它会发布一条消息,显示可用的命令。 相反,它发送 6 条消息(有些消息显示所有命令,有些只显示一两条)。
【问题讨论】:
-
您的机器人似乎有多个实例同时运行。尝试关闭所有终端和所有使机器人运行的东西,然后重试。如果您的机器人在 Heroku 之类的主机上运行,请更改您的令牌以结束所有进程。
标签: python bots discord discord.py