【问题标题】:Why doesn't my discord bot respond to any command that I give in the chat为什么我的不和谐机器人不响应我在聊天中给出的任何命令
【发布时间】:2022-10-13 03:03:33
【问题描述】:

我面临的问题是我的不和谐机器人没有响应或阅读我在聊天中写的消息。 下面的代码输出是用户名,仅此而已。

import discord
import random

TOKEN ='example'

client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print('We have logged in as{0.user}'.format(client))

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = (message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')
 
    if message.author == client.user:
        return

    if message.channel.name == 'example':
        if user_message.lower() == 'Hello':
            await message.channel.send(f'Hello {username}')
    elif user_message.lower() == 'bye':
        await message.channel.send(f'Hello {username}')
    elif user_message.lower() == '!random':
        response = f'This is your number: {random.randrange(1000000)}'
        await message.channel.send(response)
    
client.run(TOKEN)

【问题讨论】:

  • bye 之后,您缺少'
  • 此外,小写命令永远不能等于"Hello",因为它包含一个大写字母

标签: python python-3.x discord.py bots


【解决方案1】:

.lower() 方法只搜索字符串中的小写字母,因此名称,因此在聊天中输入“Hello”不会触发命令,因为“Hello”有一个大写“H”。要修复您的代码,您可以:

将您的代码更改为

if user_message.lower() == 'hello':
   await message.channel.send(f'Hello {username}')

请注意,您仍然可以保留大写字母 H 表示您好

await message.channel.send(f'Hello {username}')

或者,您可以像这样比较 2 个值:

string = 'Hello'
if user_message == string:
   #the rest of your code goes here

您的完整代码应该是:

import discord
import random

TOKEN ='exemple'

client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print('We have logged in as{0.user}'.format(client))

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = (message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message} ({channel})')
 
    if message.author == client.user:
        return

    if message.channel.name == 'example':
        string = 'Hello'
        if user_message.casefold() == string:
            await message.channel.send(f'Hello {username}')
        elif user_message.lower() == 'bye':
            await message.channel.send(f'Hello {username}')
            return
        elif user_message.lower() == '!random':
            response = f'This is your number: {random.randrange(1000000)}'
            await message.channel.send(response)
            return 
    
client.run(TOKEN)

【讨论】:

    【解决方案2】:

    Intents.default() 不包括现在需要的 Message Content 意图。如果没有意图,message.content 将是空的。

    文档中的更多信息: https://discordpy.readthedocs.io/en/stable/intents.html#privileged-intents

    【讨论】:

      【解决方案3】:

      您的机器人无法读取在公会中发送的消息的内容,因为它缺少 message_content 意图。

      您只为您的机器人提供了默认意图,其中不包括 message_content 之一。

      ```client = discord.Client(intents=discord.Intents.default())
      

      以下是您修复它的方法:

          ```intents = discord.Intents.default()
          intents.message_content = True
          client = discord.Client(intents=intents)
      

      如果您的机器人仍然无法读取公会消息的内容,您还需要在 Discord 开发者门户中打开消息内容意图:

      选择您的应用 转到“机器人”选项卡 打开“消息内容意图” 现在您的机器人应该能够读取行会中发送的消息的内容。

      【讨论】:

        猜你喜欢
        • 2021-08-26
        • 2021-08-18
        • 2021-02-25
        • 2021-06-17
        • 2021-11-21
        • 1970-01-01
        • 2021-07-26
        • 2020-12-23
        • 2020-09-09
        相关资源
        最近更新 更多