Check if the message channel is in that category - python

Can someone help me? I'm trying to make a modmail every thing is working fine, user sends a DM, bot creates a channel under "category" and if someone message inside that "category" it will be delivered to the user via DM. However I get this annoying error every time someone replies inside that "category" or user DMs the bot. I'm trying to make a category check to only do something if it's the mod mail category. Thank you in advance!
Here's my code:
async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
# User DM the bot and the bot will make a textchannel under "category" and will send his DMs there. #
if message.channel.category_id == 123456789101112:
if isinstance(message.channel, discord.TextChannel):
# Message the user who DMed the bot using the textchannel under "category", your replies will be sent to the user by the bot via DM. #
Everything is working, but I'm getting this error every time someone replies inside that textchannel "category" or user DMs the bot
Error:
if message.channel.category_id == 123456789101112:
AttributeError: 'DMChannel' object has no attribute 'category_id'

Your if-statement doesn't make much sense, you should first check for the type of the channel and then for it's category and compare it.
async def on_message(message):
# Checking if the channel is a dm or a text one
if isinstance(message.channel, discord.DMChannel):
# do your thing
elif isinstance(message.channel, discord.TextChannel):
if message.channel.category is not None:
if message.channel.category.id == your_id:
# do your thing

You should format your code properly.
Also you can check whether message.author.id is your bots id client.user.id or a user.
Read the documentation for further information.

Related

Getting message id of Discord bot

I want to send a message with my Discord bot, but since the id of this message will help me in my next job, I need to send this id to another channel, how can I do it? or can i? thanks in advance for your answers
I tried this for catch the message but is not worked i tought bot is not a "author"
#Bot.listen()
async def on_message(ctx):
if ctx.message.author.id == "1006858614369177711":
x_id = ctx.message.id
await ctx.channel.send(f"currend id : {x_id}")
You can simply await to the message inside a variable.
message = await ctx.channel.send(...)
Then you can get any property you want from that variable, like its id...

How to check the reply to a message guilded.py

I'm making a command that requires checking what the user is going to reply with to a bot's message. Here's some sample code:
async def create_channel(message):
await message.reply("Reply to this with the channel name that you want to create")
How can I get the user's reply to this message? Have done the research, but didn't find it.

How can I delete messages sent by a specific user within a specific channel using Discord.py

Hopefully this isn't a duplicate post but I've searched around and can't find anything on how to do this specifically.
I'm trying to create a Discord bot which can delete messages sent by a specific author, in a specific channel, but also checks previously sent messages.
At the moment I have the below, which adds new messages from chosen user to a list, checks if they're duplicates and if they are, deletes them.
I want to know:
A: How can I make this bot Channel specific
B: Can I have this program check old messages in that channel too and also delete them if they're duplicates?
Thanks in advance for any help and if any further info required, please let me know.
import discord
TOKEN = ('MY_TOKEN_LIVES_HERE')
client = discord.Client()
messagesSeen = []
#client.event
async def on_message(message):
if "News_Bot" in str(message.author):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
client.run(TOKEN)
Yes you can do both of those things. Since you are using on_message event you can just use message.channel to also check that channel for previously sent messages. I would also advise to change news_bot to the ID of the bot. The thing is one you implement this and clear the channel history for those messages, it won't be necessary anymore as the bot will catch the messages as soon as they are sent. So the message history is kind of a one time thing, which you can just create a cmd and run it in that channel.
messagesSeen = []
#client.event
async def on_message(message):
if 1234345 is message.author.id:
if message.content in messagesSeen:
await message.delete()
async for message in message.channel.history(limit=100):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)

Discord.py Private Messages Bot

In Discord.py how can I make my bot privately send messages to other people in a server. Like, if I want to message a person in the server who I have not friend requested yet, I can use this bot. I want it to use a slash command like
"/{user}{message}"
And the bot will display the message for them and make the user who it was sent to only see it.
How do I do this using discord.py?
You have to get the member and create a DM with them, then send whatever you want to the DM. See the following code:
member = discord.utils.get(ctx.guild.members, id = (member id)) # could also use client.get_user(ID)
dm = await member.create_dm()
await dm.send('this will be sent to the user!')

How do I grab and send the profile picture of anyone on my server using a discord.py bot?

I've been working on a discord bot using discord.py for the past few days. Now i'm trying to implement a command that displays the profile picture of anyone you mention on the server.
The problem is I can only get it to give the profile picture of the person who sends the command. I can't get it to take in a second parameter without encountering errors or the bot ignoring it.
#client.command(aliases=['getprofilepic','getprofilepicture','pfp','profilepic','profilepicture','avatar'])
async def getpfp(ctx):
await ctx.send(ctx.author.avatar_url)
The code above is a command to send the profile picture of the user who sends the command into the chat.
It ignores any extra parameters and thus can not display the profile picture of anyone else except the sender.
Changing getpfp(ctx): to getpfp(ctx, usernamepfp): and ctx.send(ctx.author.avatar_url) to ctx.send(ctx.usernamepfp.avatar_url) didn't work.
I also tried ctx.send(usernamepfp.avatar_url).
How would I implement the command such that when one types .getpfp #username in the chat it would send the profile picture of #username and not the sender?
from discord import Member
#bot.command()
async def getpfp(ctx, member: Member = None):
if not member:
member = ctx.author
await ctx.send(member.avatar_url)
You can use getpfp to display the avatar of user by passing 1) Mention/Tag 2) Display name/ Nick Name 3) Discord ID.
eg .getpfp #Someone

Categories