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!')
Related
How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code
I'm trying to make a discord bot that looks for a passphrase in the DMs it gets, then gives the user a role in my server if the passphrase is in the message. The code below runs without error, but the user doesn't get the role specified in role.
if message.guild is None:
if passphrase in message.content:
await message.channel.send("You've been verified!")
serv = client.get_guild(0000000000000000)
role = serv.get_role(0000000000000000000)
member = serv.get_member(message.author.id)
if member in serv.members:
await member.add_roles(role, reason="Member was verified via DMs.")
await message.channel.send("You've been verified!")
How do I fix this?
guild.get_member() is an Optional[Member] return, as per the documentation. It's entirely possible that this is causing the issue. Instead, use await guild.fetch_member(), as it will work regardless of whether or not you have a member cache and member intents enabled in the Developer Portal.
If you want to use get_member(), make sure that you are passing the proper intents to your bot in order to get a list of members from the guild, and make sure that the intents are enabled in the developer portal.
I was making a bot for my friend using discord.py
and I wanted to make it so that it would only work in channel which include the word ticket, made by another bot named Ticket Toll
How can I do so?
Relevant docs on text channels
Unfortunately, Discord's API does not keep track of who created the channel (which is why there's no such thing as channel.author).
One solution would be to have Ticket Toll create channels in a category, and only give your bot permissions to view this category.
However, you can easily have the bot ignore messages if the channel doesn't have "ticket" in the name, by checking channel.name. Here's an example with the on_message event:
#client.event
async def on_message(message):
if 'ticket' not in message.channel.name: return
# stuff to execute if message was sent in a channel with ticket in its name
Or as a command:
#client.command()
async def something(ctx, arg):
if "ticket" not in ctx.message.channel.name: return
# stuff to execute if the command was sent in a channel with ticket in its name
Only give the bot access to read channels where you want it to work.
I have done some research and I know to use on_member_join() to know when a user joins the server. But I am having a hard time finding out how to get the username of that user and send a message to my personal account with that username. The reason for this is because my discord server is fairly small, and I get new users every day, only I want to get notified of this that way I can personally talk to them when they join.
You can easily get the Member object by referencing it in the function definition. Link to the Discord.py reference of the on_member_join event. After that you can just use something like the fetch_user("your personal ID") method and send to your dm the member.user.
Example Code:
import discord
# Starting the client with all Intents so you can use the on_member_join() event.
Client = discord.Client(intents=discord.Intents.all())
#Client.event
def on_member_join(member):
your_user = Client.fetch_user("your id")
your_dm = await your_user.dm_channel
# Verify if there is already a DM, otherwise create the channel
if not your_dm:
your_dm = await your_user.create_dm()
await your_dm.send(member.user)
You may need to enable the members intents by putting the following code as your bot definition:
intents = discord.Intents.default()
intents.members = True
# If you're using discord.Client():
client = discord.Client(intents=intents)
# If you're using commands.Bot():
client = commands.Bot(options_here,intents=intents)
After this, you can send a DM to a user object like a channel and use member.user to get the new member's name:
def on_member_join(member):
dmUser = await client.fetch_user(579450993167564811)
await dmUser.send(member.user) # Sends member's name to dmUser via DM
i want to create a discord bot using discord.py that when a user leave the bot will delete all the messages that user sent in all channels. I know to use the event handlers, but I don t have any idea on how to delete all messages from all channels of one user, I could just put the message id's in a dictionary, but maybe there is a better way.
And as reference is this bot: https://top.gg/bot/689790568154529792
async def on_member_remove(member):
for c in member.guild.channels:
await c.purge(limit=None, check=lambda m: m.author==member)