Discord.py Delete Reference - python

I wan, that my discord.py bot delete a message, if I answer p!delete to a message.
I use this:
if message.content.lower() == 'p!delete':
message = message.reference
await message.delete()
and get this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "FILEPATH", line 303, in on_message
await message.delete()
AttributeError: 'MessageReference' object has no attribute 'delete'

the problem is you're overwriting the message object. Here is the improved code
if message.content.lower() == 'p!delete':
await message.reference.delete()

Thank you all, but it now works with this code:
deleteMessage = message.reference.message_id
deleteMessage = await message.channel.fetch_message(deleteMessage)
await deleteMessage.delete()
await message.delete()

Related

discord.py | discord.VoiceClient.play | getting an error trying to play an mp3

if '!mvp' in message.content and message.author != client.user:
print('MVP spotted.')
await discord.VoiceClient.play(self,source='audio/quotes/CypherLastKillMVP.mp3')
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Barusu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Barusu\Desktop\discordBot\main.py", line 90, in on_message
await discord.VoiceClient.play(self,source='audio/quotes/CypherLastKillMVP.mp3')
File "C:\Users\Barusu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\voice_client.py", line 554, in play
if not self.is_connected():
AttributeError: 'MyClient' object has no attribute 'is_connected'
if '!mvp' in message.content and message.author != client.user:
voice_client = discord.utils.get(client.voice_clients, guild=message.guild)
voice_client.play(*args)
If the bot is not connected to any voice channel, voice_client will be None.

Discord.py disconnect from voice channel

So I was making a bot in discord.py which joins a voice channel and disconnects from it after a delay of 3 seconds, just to test how to make it disconnect. I have done much research but couldn't find it. The bot successfully connects but isn't able to disconnect. Here is the code-
if message.content == "-p":
channel = message.author.voice.channel
await channel.connect()
time.sleep(3)
await channel.disconnect()
And here is the error I get-
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\arnha\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\arnha\Desktop\Music bot\bot.py", line 44, in on_message
await channel.disconnect()
AttributeError: 'VoiceChannel' object has no attribute 'disconnect'
Kindly help me with the same.
It is pretty unintuitive. You have to use disconnect() on the voice_client that is an attribute of a guild. Like this:
channel = message.author.voice.channel
await channel.connect()
time.sleep(3)
voice_client = message.guild.voice_client
await voice_client.disconnect()

I need help in my discord.py bot in discord

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
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.

404 NOT FOUND (error code : 1008) : Unknown message

I am making a bot in python using discord.py , i was trying delete the command when it is executed
it is working but giving me a error and the error is
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py",
line 312, in _run_event
await coro(*args, **kwargs)
File "C:/Users/Dell/Desktop/test_bot/add_role.py", line 25, in on_message
msg = await message.channel.fetch_message(761275239346339871)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\abc.py",
line 956, in fetch_message
data = await self._state.http.get_message(channel.id, id)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py",
line 243, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
After giving the error also it works , but i want to know what rises the error
code :
msg = await message.channel.fetch_message(761275239346339871)
await msg.delete()
The problem is, The message id that you inputted has already been deleted. If you want to delete the author message just do
await ctx.message.delete()
if you want to delete the inputted message by wait_for you can do this
msg = await client.wait_for('message', check=lambda message: message.author == ctx.author)
await msg.delete()
if on_message.
#client.event
async def on_message(message):
message = await message.channel.send(message here)
await message.delete()

discord bot adding roles to a user in python

I started out to make a bot that gives a role to all the users in a server. I decided to start by giving roles when a user enters a certain message in the server. This is the code I'm using:
if message.content == 'test':
member = message.author
role = discord.utils.get(member.guild.roles, name="test")
await discord.Member.add_roles(member, role)
But I'm getting an error as:
Ignoring exception in on_message
Traceback (most recent call last):
File "E:\Program File\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "E:\folder for all folders\python\program\bot.py", line 73, in on_message
await discord.Member.add_roles(member, role)
File "E:\Program File\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
I feel lost on what to do here.
here is the correct code -
if message.content == 'test':
member = message.author
role = discord.utils.get(message.guild.roles, name="test")
await member.add_roles(role)
NOTE- Make sue the server has a role named test.
If this worked, please consider accepting this as an answer. Thanks!
I'm not really good at Python but when you go in the doc it tells you that add_roles has 3 parameters roles,reason and atomic. You don't have to put member. https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles

Categories