I need the bot to delete the message from the command author, and leave the bot message. Any help will be appreciated! thank you.
I have already tried looking for a answer on google but nothing has worked
I'm kinda late here. I tried implementing this with the new 1.0 terms but couldn't make it work. If anyone has an updated version, please do tell
Edit: I found that the new best way of doing it is to just add this at the end of the function:
await ctx.message.delete()
No need for a separate delete function anymore.
You can obtain the message that called the command by passing the context with the command using the pass_context option. You can use the Client.delete_message coroutine to delete messages.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def deletethis(ctx):
await bot.say('Command received')
await bot.delete_message(ctx.message)
await bot.say('Message deleted')
bot.run('token')
Related
I am currently trying to program my own Discord Bot with Python. I have now made an "on_message" event, but "message.content" is not returning anything. No message, nothing! Can you help me?
#client.event
async def on_message(message):
if message.content.startswith("$test"):
await message.channel.send("test")
You don't have the message_content intent, so you can't read messages.
Docs: https://discordpy.readthedocs.io/en/latest/intents.html
Don't forget to enable them on your dev portal as well.
Also, instead of manually parsing messages with prefixes, consider using the built-in Commands framework that does all this for you. Docs: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html
PS "null" is not a thing in Python, it's None. And the message content isn't None/"null", it's an empty string ("").
I am trying to make a command that has an interaction like this:
User: p.poesia (text) #user
Bot: (text copied)
#user, 2021
Please consider reading the discord.py documentation.
This is the basic code I made for you to run it.
#client.command()
async def poesia(ctx, text):
await ctx.send(text + str(ctx.author))
The code segment for the mention:
hrole = discord.utils.get(ctx.guild.roles,name="Heist")
await ctx.send(hrole.mention)
I've tried switching to <#&id> type mentions as well, but no matter how I change it it doesn't ping
what it does is send a #Heist which doesn't actually ping anyone
The bot has administrator permissions by the way
I managed to get the bot to ping the role cby changing the code to this:
allowed_mentions = discord.AllowedMentions(roles = True)
hrole = discord.utils.get(ctx.guild.roles,name="Heist")
await ctx.send(hrole.mention, allowed_mentions=allowed_mentions)
for some reason the allowed reactions were turned off in the cog, this can fix it
I can't find a guide on documentation for make this possible.
I tried to use TurnContext.UpdateActivity, but I'm getting an error.
My code:
IMessageActivity responseActivity = MessageFactory.Text("Test ctm");
responseActivity.Id = userProfile.messageToDelete;
responseActivity.Conversation = turnContext.Activity.Conversation;
responseActivity.ServiceUrl = turnContext.Activity.ServiceUrl;
//await turnContext.DeleteActivityAsync(userProfile.messageToDelete, cancellationToken: cancellationToken);
await turnContext.UpdateActivityAsync(responseActivity, cancellationToken);
The last line throws the exception:
Microsoft.Bot.Schema.ErrorResponseException: 'Operation returned an invalid status code 'NotFound''
What could be wrong? Can you share any code sample?
The Emulator is built on top of Web Chat, and unfortunately, Web Chat does not support updating or deleting activities at the moment. For more details, take a look at this comment in Web Chat's source code and this open issue in the Web Chat repository for adding support for deleteActivity and updateActivity.
Hope this helps!
Okay so I have a music bot code that was working before I switched to another virtual machine provider. All the requirements are exactly the same as in my previous virtual machine because I copy and pasted everything including the requirements.txt. The bot runs normally with 0 errors until I try to run any of the commands. It gave me this error:
discord.ext.commands.errors.CommandNotFound: Command "play" is not found
I've tried rolling back to the rewrite version I started the project on,
changed #client.command to #bot.command after assigning bot = commands.Bot(command_prefix='prefix')
#I've assigned client = discord.ext.commands
#client.command(name='play', aliases=['sing'])
async def play(self, ctx, *, search: str):
#then some code
update 1: Ran it as a cog and raised:
discord.ext.commands.errors.ExtensionFailed: Extension 'music' raised an error: TypeError: cogs must derive from Cog
update 2: No idea why rolling back the rewrite version didn't work though. Perhaps I didn't do it correctly.
Just simply run it as a cog.
Note that the way cogs work has been updated recently:
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
If you still want to run it as a standalone bot,
your bot should looks like something along the lines of:
from discord.ext.commands import Bot
bot = Bot("!")
#bot.command(name='play', aliases=['sing'])
async def play(ctx, *, search: str): # Note no self
#then some code
bot.run("token")
It's important that the bot that you run is the same bot that you register the commands with. You're also passing self to your bot even though it's not in a cog, which doesn't make sense.
Okay so I found the problem.
The bot doesnt work when I try to run it as a standalone bot.
The reason using it as a cog don't work the first time was because.
the way cogs work has been changed in discord.py rewrite.
These are the changes I made:
#in cogs/music.py
class Music:
#Code
#bot.event
async def on_ready():
print('Music bot online')
to
#in cogs/music.py
class Music(commands.Cog):
#Code
#commands.Cog.listener()
async def on_ready():
print('Music bot online')
Thank you to the legendary #PatrickHaugh for helping me figure this out.