没有使用 Discord.py 重写的命令响应
No Command responses using Discord.py Rewrite
我使用下面的代码创建了一个 discord 机器人,但是当我在服务器中键入 .8ball 或 .ping 时,我没有收到任何响应,也没有收到任何类型的错误消息。但是,当我键入 "hello" 时,我确实得到了 "Hi" 的预期响应,所以我知道它已连接。这让我感到困惑,因为我已经检查了 100 次语法并且看不到任何错误。
import discord
import random
from discord.ext import commands
bot = commands.Bot(command_prefix = '.')
@bot.command()
async def ping(ctx):
await ctx.send("PONG!")
#await ctx.send(f'pong {round(bot.latency * 1000)}ms')
@bot.event
async def on_message(message):
id = bot.get_guild(521372392132706328)
if message.content.find("hello") != -1:
await message.channel.send("Hi")
elif message.content == "users":
await message.channel.send(f"""# of Members {id.member_count}""")
@bot.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)}')
bot.run('12345')
找到解决方案证明 on_message 事件导致问题:
我使用下面的代码创建了一个 discord 机器人,但是当我在服务器中键入 .8ball 或 .ping 时,我没有收到任何响应,也没有收到任何类型的错误消息。但是,当我键入 "hello" 时,我确实得到了 "Hi" 的预期响应,所以我知道它已连接。这让我感到困惑,因为我已经检查了 100 次语法并且看不到任何错误。
import discord
import random
from discord.ext import commands
bot = commands.Bot(command_prefix = '.')
@bot.command()
async def ping(ctx):
await ctx.send("PONG!")
#await ctx.send(f'pong {round(bot.latency * 1000)}ms')
@bot.event
async def on_message(message):
id = bot.get_guild(521372392132706328)
if message.content.find("hello") != -1:
await message.channel.send("Hi")
elif message.content == "users":
await message.channel.send(f"""# of Members {id.member_count}""")
@bot.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)}')
bot.run('12345')
找到解决方案证明 on_message 事件导致问题: