Discord.py send message when another user posts a message - discord

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)

Related

Discord bot with Interactions.py | on_message not receiving messages from any channel. How to listen for messages?

My bot has fully functioning /slash commands and I would also like them to be text commands and add other functionality when users send messages in chat. However, I cannot seem to get the bot to listen for messages.
import interactions
# Create the bot
Bot = interactions.Client(
token = BotToken,
scope = ServerID
)
#Bot.event
async def on_message(message):
await HandleMessage(message)
return
Bot.start()
The code never enters the on_message method.
I had a working version with the discord.py module and they are very similar. I am not sure why it does not work with Interactions.py.
Working code with discord.py:
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
client = discord.Client(intents = intents)
#client.event
async def on_message(message):
await HandleMessage(message)
client.run(Token)

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:

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("🍪")

Sending messages to the first channel. Discord.py

I need to make it so that when the bot joins a new server, it writes a specific message to the very first text channel.
I tried to do something:
#bot.event
async def on_guild_join(guild):
print("Join to " + guild.name)
guild_to_audiocontroller[guild] = AudioController(bot, guild)
await guild_to_audiocontroller[guild].register_voice_channel(guild.voice_channels[0])
for guild in bot.guilds:
await guild.text_channels[0].send(join_message)
But it doesn't want to work, how do I do it?
As long as your bot has Send Messages permissions, all you need to send a message on join is:
#bot.event
async def on_guild_join(guild):
await guild.text_channels[0].send("I have joined the server")

{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