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

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

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

问题

  1. if p_message.content == "roll":
  2. 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.

  1. import discord
  2. import random
  3. intents = discord.Intents.all()
  4. intents.message_content = True
  5. client = discord.Client(intents=intents)
  6. @client.event
  7. async def on_message(message):
  8. p_message = message
  9. print("worked")
  10. if p_message == "roll":
  11. return str(random.randint(1, 6))
  12. 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

  1. import discord
  2. import random
  3. # 将 .all 更改为 .default
  4. intents = discord.Intents.default()
  5. intents.message_content = True
  6. client = discord.Client(intents=intents)
  7. @client.event
  8. async def on_message(message):
  9. # 使用 message.content
  10. if message.content == "roll":
  11. # 向频道发送消息,说明有人输入了“roll”
  12. await message.channel.send(random.randint(1, 6))
  13. client.run('***') # 我的令牌
英文:
  1. import discord
  2. import random
  3. # change .all to .default
  4. intents = discord.Intents.default()
  5. intents.message_content = True
  6. client = discord.Client(intents=intents)
  7. @client.event
  8. async def on_message(message):
  9. # use message.content
  10. if message.content == "roll":
  11. # send message to the channel that someone types "roll"
  12. await message.channel.send(random.randint(1, 6))
  13. 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:

确定