0
votes

I am a begineer in python and discord.py and I please need help

I want my bot to say something when I type '@firstPythonProject? Bot#4320 say (message you want the bot to say) but the code has error saying

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

please help me fixing this!

#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)')

Can anyone please help me with this? I am having problems and dont understand o.o Thanks from Rick Astley

cmon why and how i fix mostly code

1
mention = f'<@!{bot.user.id}>' this part causes error. you haven't defined a bot variable. - marmeladze
how do i define a bot variable? - user15773071
@marmeladze please help? how do i define a bot variable?? - user15773071
i don't know. give more clue about bot. maybe you just need message.user.id instead of bot.user.id - marmeladze
Your bot instance is called client, not bot. Replace bot.user.id with client.user.id and you should be good - Lukas Thaler

1 Answers

1
votes

I believe you want to mention the author. Just replace bot.user.id with message.author. There is no bot variable so you ill be mostly referring to messages or channels.