Discord Bot Delete Messages in Specific Channel - python

TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.
Is there a way to use on_message and specify in an specific channel?
#client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
await message.delete()
await user.send("Yes?")
await user.remove_roles(get(user.guild.roles, "Investigator"))
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
Is it possible to make this work through a command instead of an event?
Thanks for your help. :)

The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
About this problem the clue is:
await bot.process_commands(message)
as docs says Without this coroutine, none of the commands will be triggered.
about the main question you could try using get_channel by ID and then purge it:
eg.
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1)
you can add check to purge() to check for deleting specific message:
eg.:
def check(message):
return message.author.id == [author's ID]
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1, check=check)

Related

If Discord.py Bot gets dm

Dear stackoverflow community.
I want to add a little 'Easter egg' to my discord.py bot. If one gets a DM from a user, he should reply something like "No private messages while at work". Is that somehow possible? Unfortunately, I have no idea how to do this. That's why I can't attach code. Please help me : )
You can have an on_message event, which will fire each time there is any new message which your bot can read. Then you can check if the message is a direct message:
#bot.event # could be client instead of bot for you
async def on_message(message):
if message.author == bot.user: # Don't reply to itself. Could again be client for you
return
if isinstance(message.channel,discord.DMChannel): #If you want this to work in a group channel, you could also check for discord.GroupChannel
await message.channel.send("No private messages while at work")
await bot.process_commands(message)
Also don't forget to add bot.process_commands(message) add the end of on_message so your commands still work. (Could again be client instead of bot for you)
References:
Message.channel

How do you make your bot ignore reply mentions?

In my bot, I have an event for replying to when the bot is mentioned. The issue is, that it replies when u mention the bot through replying to it. How can I fix this? Here's my code:
#commands.Cog.listener()
async def on_message(self, msg):
guild = msg.guild
prefix = get_prefix(self.client, guild)
if msg.author == self.client.user:
return
if msg.mention_everyone:
return
if self.client.user.mentioned_in(msg):
embed = discord.Embed(description=f"The prefix for this server is `{prefix}` <:info:857962218150953000>", color=0x505050)
await msg.channel.send(embed=embed)
Change
if self.client.user.mentioned_in(msg):
to
if msg.guild.me.mention in msg.content:
Why this happens
discord messages use a special field in it's messages to specify if someone is mentioned or not, if a message has someone in that field then the message will ping that someone. mentioned_in works that way so you can figure out if a certain message pings a certain person. when we use the msg.guild.mention part, we manually check for the mention instead of discord telling us if someone was mentioned since discord counts replies as mentions
I've used the following code for similar applications.
Note - Replace 'client' in client.user.id with what you've used like bot.user.id
#client.event
async def on_message(message):
if message.content in [f'<#{client.user.id}', f'<#!{client.user.id}>']:
await message.channel.send("Hey! My prefix is ```,```")
await client.process_commands(message)

Discord.py | Publish messages

does someone know how to publish a message by a user? I have already tried a bit and asked on a discord coding server, but I didn't got a good answer.
I tried something like this:
#client.event
async def on_message(message):
if message.channel.type == discord.ChannelType.news:
await message.publish
return
I don't yet have the reputation to reply to Phoenix Doom directly.
However I think bendix9009 is likely referring to the 'Publish' option that you get when typing messages into 'Announcement' text channels.
When you or a bot send a message into these channels, they are not published to any other channels that are following them by default.
It'd be useful to know whether you can just add some sort of flag onto the below line to publish those messages automatically.
client.channels.cache.get(channelId).send; (Mine's written in javascript)
Today I had the same problem and I fixed it with:
#client.command()
async def sendMessage(ctx, message):
msg = await ctx.send("Published message: " + message)
await msg.publish()
So I think you forgot the brackets. So this code would work:
#client.event
async def on_message(message):
if message.channel.type == discord.ChannelType.news:
await message.publish()
return

How to make my bot forward DMs sent to it to a channel

So, I've got a bot that can send a user a DM via a very simple command. All I do is "!DM #user message" and it sends them the message. However, people sometimes try responding to the bot in DMs with their questions, but the bot does nothing with that, and I cannot see their replies. Would there be a way for me to make it so that if a user sends a DM to the bot, it will forward their message to a certain channel in my server (Channel ID: 756830076544483339).
If possible, please can someone tell me what code I would have to use to make it forward on all DMs sent to it to my channel 756830076544483339.
Here is my current code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
activity = discord.Game(name="DM's being sent", type=3)
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="Pokemon Go Discord Server!"))
print("Bot is ready!")
#bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
await ctx.send('DM Sent Succesfully.')
Thank you!
Note: I'm still quite new to discord.py coding, so please could you actually write out the code I would need to add to my script, rather than just telling me bits of it, as I often get confused about that. Thank you!
What you're looking for is the on_message event
#bot.event
async def on_message(message):
# Checking if its a dm channel
if isinstance(message.channel, discord.DMChannel):
# Getting the channel
channel = bot.get_channel(some_id)
await channel.send(f"{message.author} sent:\n```{message.content}```")
# Processing the message so commands will work
await bot.process_commands(message)
Reference

How to read reactions from a DM channel on discord using discord.py

While programming a discord bot, I realized that I needed to read reactions that the bot dmed to different users.
Code:
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send('{} has added {} to the message: {}'.format(user.name, reaction.emoji, reaction.message.content))
This reads a message from basically any channel that the bot is in (e.g. in guilds), as opposed to just DM channels. Is there a fix for this?
Thanks in advance
I came to this realization just before so I'll add it as an answer.
You can just use isinstance to check the channel type as dpy has different internal class's for all channels.
#bot.event
async def on_reaction_add(reaction, user):
if not isinstance(reaction.message.channel, discord.DMChannel):
# Not a dm
return
print("Hey this should be a dm")
That code will only ever run in dms.
Alternately, you could remove the not and put your dm code within the if statement

Categories

Resources