Discord Bot 使用 python 向一个命令发送多个响应
Discord Bot using python sending multiple responses to one command
我正在 python 中使用 atom 为 discord 编写一个机器人程序,每当我 运行 我的机器人程序上的一个命令(包括帮助)时,它都会向命令发送多个响应,而不是所有的响应是相同的(例如: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
我看不出有任何理由让它像以前那样循环。您的任何齿轮中是否有任何 pong 命令?我已经更正了您的缩进,运行 没有遇到同样的问题也没问题。
我让机器人 运行 多次 smh(几天前修复)
我正在 python 中使用 atom 为 discord 编写一个机器人程序,每当我 运行 我的机器人程序上的一个命令(包括帮助)时,它都会向命令发送多个响应,而不是所有的响应是相同的(例如: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
我看不出有任何理由让它像以前那样循环。您的任何齿轮中是否有任何 pong 命令?我已经更正了您的缩进,运行 没有遇到同样的问题也没问题。
我让机器人 运行 多次 smh(几天前修复)