Discord.py 在 on_message 客户端事件上识别,但 IF 语句不响应。

huangapple go评论80阅读模式
英文:

Discord.py recognizing on_message client event, but IF statements not responding

问题

if p_message.content == "roll":
    return str(random.randint(1, 6))
英文:

I am working on a Discord bot and in this case want the bot to roll a 6 sided die when any user gives the "roll" command. See code below. Oddly enough this was working just fine, but now the print(worked) triggers and the if p_message statements do nothing. I get no errors, it just doesn't work.

import discord
import random

intents = discord.Intents.all()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
  p_message = message
  print("worked")

  if p_message == "roll":
    return str(random.randint(1, 6))

client.run('***') #my token

I have tried ensuring that message is a str but it makes no difference. Again, this code worked previously (at 1am when I was probably too tired to be writing code) and throws no error. It just doesn't respond to the command at all in discord.

答案1

得分: 1

    import discord
    import random

    # 将 .all 更改为 .default
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    
    
    @client.event
    async def on_message(message):
        # 使用 message.content
        if message.content == "roll":
            # 向频道发送消息,说明有人输入了“roll”
            await message.channel.send(random.randint(1, 6))
    
    
    client.run('***')  # 我的令牌
英文:
import discord
import random

# change .all to .default
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def on_message(message):
    # use message.content
    if message.content == "roll":
        # send message to the channel that someone types "roll"
        await message.channel.send(random.randint(1, 6))


client.run('***')  # my token

huangapple
  • 本文由 发表于 2023年3月31日 22:06:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定