Discord.py bot doesn't dm user - discord

If someone says hey in chat I want the bot to dm them, but it gives absolutely no errors an doesnt work either
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.lower == "hey":
await message.author.send("heyo")
await message.add_reaction(":b:")
await client.process_commands(message)

I tried tinkering with your code and something I noticed was that you weren't calling the lower function correctly. lower should be lower(). That allowed the bot to at least DM me.
(Note: I also fixed the add_reaction error by using the write emoji format needed)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.lower() == "hey":
await message.author.send("heyo")
await message.add_reaction("🅱️")
await client.process_commands(message)

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 looping messages up to infinity

#client.event
async def on_message(message):
if message.author == client:
return
if message.channel.id == 885569828436447254:
await message.channel.send(bgmi)
await message.channel.send(news)
I am just trying to ping the role which is stored in variable bgmi for each message in a particular channel and it works!
But the glitch is that every time a message is received, the bot sends both the messages again and again until the bot is stopped. Please help me configure it and make it send the messages only a single time.
#client.event
async def on_message(message):
if message.guild is None or message.author == message.guild.me:
return
if message.channel.id == 885569828436447254:
await message.channel.send(bgmi)
await message.channel.send(news)
You should use Guild.me. In a guild, message.author is of Discord type Member, while client is just the python type used to manage the communication between discord. What you actually want is Guild.me
This works only if the message is sent in a discord server, for private channels you'll need to do otherwise
Replace if message.author == client: with if message.author == client.user:

How do I check to see if the channel id is equal to something

I'm trying to make and event that checks to see if the channel id is equal to a specific id, if it is then the bot adds a reaction to that message. I'm not quite sure how to do this and have looked up many solutions but none of them help. There are some errors in the code that I still have to figure out like channel.id is not and actual command. Here is my code:
#client.event
async def on_message(message):
await discord.Client.get_channel(<channel_id>)
if channel.id == <channel_id>:
await message.add_reaction("✔️")
await message.add_reaction("❌")
discord.Client is a type, not a instance. And there is a message property inside discord.Message.
#client.event
async def on_message(message):
if message.channel.id == <channel_id>:
await message.add_reaction("✔️")
await message.add_reaction("❌")

Discord.py bot react to user's next message after using a specific command

For example:
I use !react command and then after 1 min I send a message and then the bot reacts to the message with a specific emoji. Is it possible?
#commands.command()
#commands.cooldown(1, 600, commands.BucketType.user)
async def cookieme(self, ctx):
...somehow remembers to this name and next time the user say something it reacts to the msg with a cookie
I don't really understand your question but I will try to help you:
#bot.command()
async def react(ctx):
def check(message):
return ctx.author == message.author and ctx.channel == message.channel
react = await bot.wait_for("message", check=check)
await react.add_reaction("🍪")

Discord reaction_add doesn't work in Direct Message channel

I've been troubleshooting this thing for days and this is the last straw.
Reaction_add only works if I do !on_message in the server 'general' channel, if I directly message the bot, it doesn't do anything. Though, funny thing is, if I message bot directly first and the enter !on_message in server general channel, bot reacts in general channel and in direct messages.
After TimeoutError I get thumbs down in direct messages.
This is the code, straight from discord.py documentation and I'm using Bot as Client:
#bot.command(pass_context=True)
async def on_message(ctx):
channel = ctx.channel
await ctx.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) == '👍'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')
It's funny that wait_for(message, ...) message works just fine everywhere.
Tried adding this and still no luck.
intents = discord.Intents.default()
intents.dm_reactions = True
bot = commands.Bot(command_prefix=PREFIX, description=DESCRIPTION, intents=intents)
It's because on_message is a bot event, which means it does not need to be invoked as a command, you can call the command with (), but in your case, you are trying to use the command as a event.
This is an event
#bot.event
async def on_message(message):
channel = message.channel
await message.channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await message.channel.send('👎')
else:
await message.channel.send('👍')
This is a command, a command needs to be invoked with a function, in this case, it would be your prefix + thumbs !thumbs hope both of these help.
#bot.command()
async def thumbs(ctx):
channel = ctx.channel
await ctx.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) == '👍'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await ctx.send('👎')
else:
await ctx.send('👍')
I added intents = discord.Intents.all() and that seems to do the trick, though it's not efficient because I might not need all privileged intents.
Input on issue would be helpful.

Resources