【发布时间】: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
【问题讨论】:
-
你能正确缩进吗?
标签: python bots discord discord.py discord.py-rewrite