我需要帮助解决我的矛盾



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

我想让我的机器人在我键入'@firstPythonProject? Bot#4320 say (message you want the bot to say)时说些什么,但代码在说时出错

C:UsersArshdeep SinghDesktopfirstPythonProject_ 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:UsersArshdeep SinghAppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersArshdeep SinghDesktopfirstPythonProject_ Botbot.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:UsersArshdeep SinghAppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersArshdeep SinghDesktopfirstPythonProject_ Botbot.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:UsersArshdeep SinghAppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersArshdeep SinghDesktopfirstPythonProject_ Botbot.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:UsersArshdeep SinghAppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersArshdeep SinghDesktopfirstPythonProject_ Botbot.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:UsersArshdeep SinghAppDataRoamingPythonPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersArshdeep SinghDesktopfirstPythonProject_ Botbot.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)')

有人能帮我吗?我有问题,不明白感谢Rick Astley

关于为什么以及如何修复主要代码

我相信你想提到作者。只需将bot.user.id替换为message.author即可。没有机器人变量,所以你主要指的是消息或频道。

最新更新