my code is
#bot.command()
async def on_message(message):
if 'Congratulations' in message.content:
await message.delete(message)
await bot.process_commands(message)
However whenever I type Congratulations the message wont get deleted
Your code provided in your question is a command, it would not be listening for an on_message event, as the command must be invoked by a user.
This is an event applied to your code, it would always wait for a message, only if the message contains "Congratulations", will trigger the message delete
#bot.event
async def on_message(message):
if 'Congratulations' in message.content:
await message.delete()
await bot.process_commands(message)
Do await message.delete() instead of await message.delete(message).
Related
I have tried to convert my discord bot's commands to using ctx instead of interactions but the code I have tried below does not work.
#bot.command(description="Says test")
async def test(ctx):
await ctx.send("Test")
My issue is that the command never loads.
Updated code but still broken
import discord
from discord import ui
from discord.ext import commands
bot = commands.Bot(command_prefix="/", intents = discord.Intents.all())
# Run code
#bot.event
async def on_ready():
print('The bot has sucuessfully logged in: {0.user}'.format(bot))
await bot.change_presence(activity=discord.Activity(type = discord.ActivityType.listening, name = f"Chilling Music - {len(bot.guilds)}"))
# Commands
#bot.command(description="Says Hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}!")
#bot.command(description="Plays music")
async def play(ctx, song_name : str):
await ctx.author.voice.channel.connect()
await ctx.send(f"Now playing: {song_name}")
#bot.command(description="The amount of servers the bot is in")
async def servercount(ctx):
await ctx.send(f"Chill Bot is in {len(bot.guilds)} discord servers!")
#bot.command(description="The bots ping")
async def ping(ctx):
await ctx.send(f"🏓Pong\nChill Bot's ping is\n{round(bot.latency * 1000)} ms")
#bot.command(description="Announcement command")
async def announce(ctx, message : str):
await ctx.send(message)
#bot.command(description="Support Invite")
async def support(ctx):
await ctx.send(f"{ctx.author.mention} Please join our server for support! [invite]", ephemeral=True)
#bot.command(description = "Syncs up commands")
async def sync(ctx):
await ctx.send("Synced commands!", ephemeral=True)
bot.run('')
Your commands don't work because you incorrectly created an on_message event handler.
By overwriting it and not calling Bot.process_commands(), the library no longer tries to parse incoming messages into commands.
The docs also show this in the Frequently Asked Questions (https://discordpy.readthedocs.io/en/stable/faq.html#why-does-on-message-make-my-commands-stop-working)
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message.
Also, don't auto-sync (you're doing it in on_ready). Make a message command and call it manually whenever you want to sync.
If someone mentions me how do I get the bot to read that? like if someone mentions me I want the bot to say something back. How would I do that?
#client.event
me = '<#user_id>'
async def on_message(message):
member = message.author.id
if message.content == me:
await message.channel.send('my master will be back shortly')
else:
return
await client.process_commands(message)
You can use discord.Message.mentions. It returns a list of discord.Member that were mentioned in message.
#client.event
async def on_message(message):
me = message.guild.get_member(<user id>)
if me in message.mentions:
await message.channel.send('my master will be back shortly')
await client.process_commands(message)
Reference
discord.Message.mentions
I'm trying to make an on_message event where if an admin is mentioned it will delete that message and then tell them they can't do that. Here is my code right now
#bot.event
async def on_message(message):
admin_id = "<#496186362886619138>"
if admin_id in message.content:
await message.delete()
await message.channel.send("You can't do that")
await bot.process_commands(message)
Took another look at the documentation and found mentioned_in() and Client.fetch_user().
My final code looks like this:
#bot.event
async def on_message(message):
# DELETE ADMIN MENTION
user = await bot.fetch_user(496186362886619138)
if user.mentioned_in(message):
await message.delete()
await message.channel.send("You can't do that")
await bot.process_commands(message)
I created a lot of moderation commands, and only allowed admins to use them.I also really want to make such a scheme:If a person who does not have the right to use the command writes it,the bot sends a message that you do not have the right to use this command.Please help me figure this out!
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
You can use a error handler.
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('You do not have the required permissions to use this command.')
How can I check for the user reaction? I'm using that code:
#client.command()
async def react(ctx):
message = await ctx.send("Test")
await question.add_reaction("<💯>")
await question.add_reaction("<👍>")
How can I do an action if the user react to the message with 💯 and another action if the user react to the message with 👍? Thank you in advance
In the documentation you can find client.wait_for() which waits for an event to happen. The example from the documentation should help you out:
#client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')