I wanted to ask how do I make my bot react to a message if it contains something, for example:
if the message contains "Hi" the bot will react with :wave:.
so how do I do this? any help is appreciated, I am new to the forum so sorry if I made any mistakes. :)
You should try this
#client.event
async def on_message(message):
if 'Hi' in message.content.split():
await message.add_reaction("👋")
It will probably work
if you use cogs :
#commands.Cog.listener()
async def on_message(self,message):
if 'Hi' in message.content.split():
await message.add_reaction("👋")
Related
I'm trying to create a simple discord bot that replies with mention to messages that contain certain words.
Also, I want the bot to respond only when it gets mentioned and without a prefix.
This was my try, but the bot didn't reply at all to the messages.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='')
#client.event
async def on_message(message):
if client.user.mentioned_in(message){
if message.content == "Hi"{
await message.channel.send(f"Hello {message.author.mention}")
}
if message.content == "How are you?"{
await message.channel.send(f"I'm good {message.author.mention}")
}
}
client.run("TOKEN")
message.content contains the entirety of the message text so when you do #mybot Hi, the message.content will be something like: <#MY_BOT_ID> hi. Your code is checking that the message.content is exactly equals to either Hi or How are you? and that is not going to be the case.
You could use Python's in operator to check for certain text:
#client.event
async def on_message(message):
if client.user.mentioned_in(message):
# making the text lowercase here to make it easier to compare
message_content = message.content.lower()
if "hi" in message_content:
await message.channel.send(f"Hello {message.author.mention}")
if "how are you?" in message_content:
await message.channel.send(f"I'm good {message.author.mention}")
client.run("TOKEN")
Though, this isn't perfect. The bot will also respond to anything with the characters hi in. You could split the content of the messages by word and check that or you could use regular expressions. See this post for more info.
Additionally, your python code had some {} brackets - which is invalid syntax for if statements - I've corrected that in my example. Consider looking up python syntax.
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!')
I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)
I've been working on this piece of code:
#client.event
async def lol(message,ctx):
if ctx.author.id == <user_id>:
await message.add_reaction('❤️')
else:
return
I am pretty sure that it is developed correctly, yet I am not getting the desired results.
I am pretty sure that if you check the console, an HTTP error would have been encountered. That's because you directly pasted the emoji in the function parameter. You have to add its unicode value. In your case:
await message.add_reaction(u'\u2764')
You are using an event decorator rather than a command, unless you were trying to detect if the user reacted, but its still completely off. What it looks like, the command that is called will send a message and the bot will react to it.
#client.command()
async def lol(ctx):
message = await ctx.send(f"{ctx.author.mention}, lol")
await message.add_reaction('😂')
If you then want to detect if the user had reacted to the emoji, you can use a event, but an on_reaction_add:
#client.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '😂':
await user.send("Lol")
I'm trying to create a playlist which the bot can send but I can't figure it out, here's the code I've tried although it only takes the text to trigger the command and I can't access the list anywhere.
Code:
#client.event
async def on_message(message):
if message.content == "=playlist":
playlist = message.content
await message.channel.send(playlist)
Any ideas?
I don't understand what the problem is, by the looks of it, when a users message is =playlist, your code will send all the contents of that message essentially back to them. Is that not what you want?