Sending a message when prefix was send discord.py - discord

I was making a discord bot with the prefix "!c", but I wanted that if people sent "!c" that an embed showed up so I fixed it by doing this:
client = commands.Bot(command_prefix='!')
client.remove_command("help")
#client.group()
async def c(ctx):
YourEmbedCodeHere
I want to add a command "!c help" that it sends the same embed. I tried it by doing:
#c.group(invoke_without_command= True)
async def help(ctx):
embed = discord.Embed(title="ChiBot", description="ChiBot by Chita! A very usefull moderation, level, invite and misc bot!", color=0x62e3f9)
embed.add_field(name="Commands:", value="Do !help (command) to get help on that command!", inline=False)
embed.add_field(name="Misc", value="!c help !c randomimage !c invite !c echo", inline=False)
embed.add_field(name="Moderation", value="!c ban !c kick !c warn !c mute !c unmute !c warns !c warnings", inline=False)
embed.add_field(name="Levels", value="!c rank !c dashboard ", inline=False)
embed.add_field(name="Invites", value="!c invites (help with setting it up)", inline=False)
embed.set_footer(text="Created by Chita#8005")
await ctx.send(embed=embed)
But when I try that, it sends double because "!c" is still a command for that same embed. How can I make it so it only sends 1 embed? And I want to add other "!c" commands as well so I need a solution to remove the !c embed if there is something behind the !c
Regards,
Chita

You can use the on_message event listener to check for that.
#client.listen('on_message')
async def foo(message):
await client.wait_until_ready()
ctx = await client.get_context(message)
if message.content == '!c':
await ctx.send(embed=embed) # send your embed here
return #makes sure that we dont reply twice to `!c`
we are using client.wait_until_ready() so that the client doesn't respond to messages before it is connected.
Optionally, you can add
if message.author.bot:
return
to make sure that we don't reply to bots.
Resources:
on_message
wait_until_ready

You can try having the prefix stay as "!c" and have an on_message event to listen for a "!c" to send the embed. Something like this.
#client.event
async def on_message(message):
if message.content == "!c":
#Your embed code here
await client.process_commands(message)
I hope this may be of help.

Related

How can I have a trigger without my command prefix, like welcome and not $welcome?

So I wanted to make a feature on my discord bot and I wanted it to respond when users say welcome and say Welcome! back. But every time I try it only responds to $welcome. ($ is my prefix). What should I do?
client = commands.Bot(command_prefix = "$")
#client.command()
async def welcome(ctx, message):
if any(word in message for word in welcome):
await message.channel.send("Welcome!")
#client.event
async def on_message(message):
if "welcome" in message.content:
await message.channel.send('Welcome!')

discord.py sending message to channel name instead of ID

I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?
Code:
#client.event
async def on_command_completion(ctx):
channel = client.get_channel('829067962316750898')
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
await channel.send(embed=embed)
You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.
#client.event
async def on_command_completion(ctx):
channel = discord.utils.get(ctx.guild.text_channels, name='logs')
if channel is None:
return # If there is no logs channel, do nothing (or what you want to do)
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{channel.name} **( {channel.mention} )**")
await channel.send(embed=embed)
See the discord.TextChannel docs and discord.utils.get()

Discord.py send message when another user posts a message

I am making a bot in discord.py and want my bot to ping a role when a Webhook sends message in a specific channel. Is there anyway to do this? Right now all I have is the channel id and I am pretty sure it is a client Event
#client.event
async def pingrole():
channel = client.get_channel("channel id")
async def on_message(message):
if message.author.id == webhook.id:
await ctx.send(role.mention)

Discord.py ignore specified channel

I've got this code which logs deleted messages to a channel in my discord. However, I'd like to know how I could make it ignore one specified channel, which I do not wish for it to log deleted messages from. What would I need to edit into my code to do this? Thanks.
#bot.event
async def on_message_delete(message):
embed=discord.Embed(title="{} deleted a message".format(message.author), description=" ", color=0x55246c)
embed.add_field(name= message.content ,value="Message logging coded by ProfessorAdams.", inline=True)
channel=bot.get_channel(CHANNEL_ID)
await channel.send(embed=embed)
If you could let me know what to add to this code to make it ignore one channel, but work for every other channel, that would be amazing. Thank you!
You can check if the deleted message channel id is equal to the channel id that you want it to ignore.
#bot.event
async def on_message_delete(message):
if message.channel.id == <IGNORED_CHANNEL_ID>: #Enter the channel id that you want to ignore
return
embed=discord.Embed(title="{} deleted a message".format(message.author), description=" ", color=0x55246c)
embed.add_field(name= message.content ,value="Message logging coded by ProfessorAdams.", inline=True)
channel=bot.get_channel(CHANNEL_ID)
await channel.send(embed=embed)

{discord.py} Delete a specific message from a specific user

I want to delete a specific message from a specific user using discord.py
The user's id is: 462616472754323457
The message that he can't send: lmao
So if he sends lmao, it deletes the message and send "You can't do that <#462616472754323457>
Try discord.TextChannel.purge.
#client.event
async def on_message(message):
if len(await message.channel.purge(limit=200, check=lambda x: ('lmao' in x.content.strip().lower()) and x.author.id == 462616572754323457)) > 0:
await message.channel.send('You are not allowed to do that, <#462616572754323457>')
Accounting for when the bot is offline, the bot will check 200 messages before the current message and delete any that are from 462616572754323457 and have 'lmao' in the contents. (You can further enhance this by using re.)
You can use the on_message event, which triggers whenever a message is sent.
#bot.event
async def on_message(message):
await bot.process_commands(message) # add this if also using command decorators
if message.author.id == 462616472754323457 and "lmao" in message.content.lower():
await message.delete()
await message.channel.send(f"You can't do that, {message.author.mention}")
References:
discord.on_message()
Message.delete()
Bot.process_commands()
Member.mention

Resources