【问题标题】:I need help in my discord.py bot in discord我在不和谐中的 discord.py 机器人中需要帮助
【发布时间】:2021-07-20 11:16:51
【问题描述】:

我是 python 和 discord.py 的初学者,我需要帮助

我希望我的机器人在我输入 '@firstPythonProject? Bot#4320 say (message you want the bot to say) 时说些什么,但代码有错误提示

C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot>python bot.py
Logged on as firstPythonProject? Bot#4320
firstPythonProject? Bot#4320 has connected to Discord!
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Arshdeep Singh\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot\bot.py", line 28, in on_message
    mention = f'<@!{bot.user.id}>'
NameError: name 'bot' is not defined
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Arshdeep Singh\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot\bot.py", line 28, in on_message
    mention = f'<@!{bot.user.id}>'
NameError: name 'bot' is not defined
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Arshdeep Singh\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot\bot.py", line 28, in on_message
    mention = f'<@!{bot.user.id}>'
NameError: name 'bot' is not defined
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Arshdeep Singh\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot\bot.py", line 28, in on_message
    mention = f'<@!{bot.user.id}>'
NameError: name 'bot' is not defined
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Arshdeep Singh\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Arshdeep Singh\Desktop\firstPythonProject_ Bot\bot.py", line 28, in on_message
    mention = f'<@!{bot.user.id}>'
NameError: name 'bot' is not defined

请帮我解决这个问题!

#code
# bot.py
import random
import discord
import time

client = discord.Client()

class MyClient(discord.Client): 
    async def on_ready(self):
        print('Logged on as', self.user)
        print(f'{client.user} has connected to Discord!')
        
    async def on_message(self, message):
        # don't respond to ourselves
        if message.author == self.user:
            return

        if message.content == 'ping':
            await message.channel.send('pong')
            
        if message.content.lower() == 'pog':
            await message.channel.send('yes pog')

        if 'rickroll' in message.content.lower() or 'rick roll' in message.content.lower():
            await message.delete()
            await message.channel.send(f'@{message.author.mention}, no bad')

        mention = f'<@!{bot.user.id}>'
        
        if mention in message.content:
            if '{mention} say ' in message.content:
                sayOutput = message.content.replace('{mention} say ', message.content)
                await message.channel.send(f'{message.author.mention} asked me to say:\n',
                                           sayOutput)
                                       
                                       
                                      

client = MyClient()
client.run('no token 4 u (no its not because of invalid token other commands work)')

谁能帮我解决这个问题? 我有问题,不明白 o.o 感谢 Rick Astley

cmon 为什么以及如何修复大部分代码

【问题讨论】:

  • mention = f'&lt;@!{bot.user.id}&gt;' 这部分会导致错误。你还没有定义机器人变量。
  • 如何定义机器人变量?
  • @marmeladze 请帮忙?如何定义机器人变量??
  • 我不知道。提供更多关于bot 的线索。也许你只需要message.user.id 而不是bot.user.id
  • 您的机器人实例被称为client,而不是bot。将bot.user.id 替换为client.user.id,你应该会很好

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


【解决方案1】:

我相信你想提到作者。只需将bot.user.id 替换为message.author。没有 bot 变量,因此您主要指的是消息或频道。

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2021-01-16
    • 2018-11-17
    • 2021-07-03
    • 2018-04-29
    • 2021-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多