I'm trying to dm a mentioned user in discord.
I need something like
class MyClient(discord.Client):
# ...
client = MyClient()
client.run("Token")
not discord.ext because it would ruin my code.
I tried:
if message.content.lower().startswith("/trade"):
mention = message.author.mention
await client.send(mention, message.author + " will mit dir traden!")
but it doesn't work.
I don't really recommend using / as a prefix due to the potential confusion of the built in slash commands (coming in d.py 2.0 for bots).
You probably are better using ext.commands eventually though as you can type hint the user much easier within the command signature.
e.g.
#commands.command()
async def trade(self, ctx, member: discord.Member):
# Code here
await member.send("message")
In your example though you will want to use message.mentions to get a list of users mentioned in the message. If you want just the first mention then you can use the index 0 message.mentions[0]
mentioned_member = message.mentions[0]
await mentioned_member.send("Message")
Related
NOTE: I am using discord.Client for my code.
So, when we are sending a message, we can use delete_after=secs parameter to delete a message after some time.
channel = await message.author.create_dm()
await channel.send("test dm (deletes after 60 secs)", delete_after=60)
But, do we have thing like that with discord.Role, or is there another solution to remove a role after some time?
This is docs for discord.Role.delete.
If you look there, you can see that it is not build in, so you'd have to implement the solution yourself.
Luckly its not too bad.
import asyncio
...
await asyncio.sleep(delay)
await discord.Role.delete(discord.Role.id, reason=None)
Hope this helps.
I maked some searchs and didn't find a way or something good to help me...
I wanna make a bot in python that send a DM to the User's ID's that i have. I looked for the Discord api py, but didn't understanded how can make the function to send message.
There are several ways to DM a user in discord.py.
If you want to message the user who ran the command, you can use ctx.author.send:
#client.command()
async def send(ctx):
await ctx.author.send("Hi there")
To message a specific user:
#client.command()
async def send(ctx, user : discord.Member):
await user.send("Hello")
Note: The user parameter can be either an #mention or, in your case, a user ID.
Try this. Just do something like
#bot.command(pass_context=True)
async def send(ctx,user:discord.Member):
await user.send("Hello User"!)
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)
I have tried different options, and this one was the one that I desired the most, but I cannot seem to get it working. Could I get some help? (Sorry I am very new to coding)
This is my code:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='-')
#bot.command()
async def speak(ctx, *, text):
if ctx.message.author.id == ID here:
message = ctx.message
await message.delete()
await ctx.send(f"{text}")
else:
await ctx.send('I need text!')
Thanks
Your else statement makes little sense here. It is best to set up the condition differently, that you want text once and if that does not happen, then there is an else argument.
I don't know if this is relevant either, but apparently it looks like you want only one person to be able to execute this command. For the owner there would be:
#commands.is_owner()
But if you want to make it refer to another person use:
#bot.command()
#commands.is_owner() # If you want to set this condition
async def say(ctx, *, text: str = None):
if ctx.message.author is not ID_You_Want:
await ctx.send("You are not allowed to use the command.")
return # Do not proceed
if text is None: # If just say is passed with no text
await ctx.send("Please insert a text.")
else:
await ctx.send(text) # Send the text
Optional: You can also delete the command that was send with ctx.message.delete()
Your command is not executing because you defined client and bot.
Simply remove client = discord.Client() and you will be fine.
Say I want to make a bot with a "poke" feature (aka sends a pm to a user saying "Boop" when someone says "!poke #user#0000"), how would I do this? It works perfectly when I do this:
#bot.command(pass_context=True)
async def poke(ctx, message):
await client.send_message(ctx.message.author, 'boop')
but only if I want to poke the author of the message. I want to poke whoever's being #'d.
I know the discord.py documents say I can use this:
start_private_message(user)
but I don't know what to put in place of user.
It's actually simpler than that
#bot.command(pass_context=True)
async def poke(ctx, member: discord.Member):
await bot.send_message(member, 'boop')
send_message contains logic for private messages, so you don't have to use start_private_message yourself. The : discord.Member is called a converter, and is described in the documentation here
I might be necroing this post a bit, but the recent version of this would look like this:
#bot.command():
async def poke(ctx, user: discord.Member=None):
if user is None:
await ctx.send("Incorrect Syntax:\nUsage: `!poke [user]`")
await user.send("boop")
The if user is None: block is optional, but is useful if you want to have an error message if a command isn't used correctly.