Discord Bot 使用 python 向一个命令发送多个响应
Posted
技术标签:
【中文标题】Discord Bot 使用 python 向一个命令发送多个响应【英文标题】:Discord Bot using python sending multiple responses to one command 【发布时间】:2020-02-15 20:19:54 【问题描述】:我正在使用 atom 在 python 中编写一个不和谐的机器人,每当我在我的机器人上运行命令(包括帮助)时,它都会向命令发送多个响应,并非所有响应都是相同的(例如:s?ping它会用不同的 ping 多次回答)。我认为它发送的数量是随机的,我根本不确定它有什么问题,我听说循环这样做但我只有一个循环,那就是改变状态。这是我的代码(没有令牌):
import discord
import random
import os
from itertools import cycle
from discord.ext import commands, tasks
client = commands.Bot(command_prefix = 's?')
status = cycle(['s? // Senkuu by Rubix', 's?help - Made by Rubix#8166', 's? // Alpha v0.11.2, Made in Python (discord.py/atom)'])
@client.event
async def on_ready():
change_status.start()
print( 'Bot is ready. ')
@tasks.loop(seconds=20)
async def change_status ():
await client.change_presence(activity=discord.Game(next(status)))
@client.command()
async def ping(ctx):
await ctx.send(f'Pong! round(client.latency * 1000) ms')
@client.event
async def on_command_error(ctx, error):
if isinstance(error,commands.MissingRequiredArgument):
await ctx.send('Missing Requirements in Command, Try Again.')
@client.command(aliases=['8ball'])
async def _8ball(ctx, *, question):
responses = ["It is certain.",
"It is decidedly so.",
"Without a doubt.",
"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.",
"Cannot predict 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)')
@client.command()
@commands.has_role('Moderator [Staff]')
async def purge(ctx, amount : int):
await ctx.channel.purge(limit=amount)
@client.command()
@commands.has_role('Moderator [Staff]')
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f'Banned member.mention')
@client.command(pass_context=True)
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, user: discord.User, *, reason):
server = ctx.message.server
mention = user.mention
id = user.id
author = str(ctx.message.author)
x = discord.utils.get(server.channels, name="criminal-records????")
kick_message = "**Type: Kick**\n**User:** " + user.name + "#" + user.discriminator +"(" + id + ")" \
+ "(" + mention+")\n" \
"**Reason:** " + reason +"\n" \
"**Responsible Moderator: **" + author
await self.bot.kick(user)
await self.bot.send_message(x, kick_message)
@client.command()
@commands.has_role('Moderator [Staff]')
async def unban(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned user.mention')
return
@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]')```
这就是它正在做的事情:https://i.stack.imgur.com/9Cisk.png
【问题讨论】:
你能正确缩进吗? 【参考方案1】:我看不出有任何理由让它像以前那样循环。您的任何齿轮中是否有任何 pong 命令?我已经更正了您的缩进并运行它没问题没有遇到同样的问题。
【讨论】:
我现在没有 cogs,只有主 bot 文件,post 中的那个,唯一的 ping 命令在那里 从上面复制并粘贴代码并添加您的令牌,看看它是否工作正常,测试时它运行良好。 这很奇怪,仍然发生在我身上。你在测试什么? 我正在使用 Visual Studio 代码,我确实删除了 ``` @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]') ``` 因为没有 cogs 可以加载 我使用 Atom 运行它是否重要?会有区别吗?【参考方案2】:我让机器人运行了多次 smh(几天前修复)
【讨论】:
以上是关于Discord Bot 使用 python 向一个命令发送多个响应的主要内容,如果未能解决你的问题,请参考以下文章
在 Python Discord Bot 中按名称而不是 ID 向特定文本通道发送消息