Extract Discord user ID from a discord message - python-3.x

I am working on a python bot, and one thing I need is for the bot to see if someone #'d another person (i.e. I type something like "!callout #randomuser") in a discord channel. If it detects this, I want to get the user ID of the #'d user (in this case, the user ID of #randomuser)
I know this is probably a dumb question, but was unable to find an answer elsewhere. Thanks!

The easier way is to use the converters that come with the discord.ext.commands extension. That can give you a User object, which has a User.id attribute:
from discord.ext.commands import Bot
from discord import User
bot = Bot(command_prefix='!')
#bot.command(pass_context=True)
async def getid(ctx, user: User):
await bot.say("Their ID is {}".format(user.id))
#bot.command(pass_context=True)
async def callout(ctx, user: User):
await bot.say("{}, someone is talking about you".format(user.mention))
bot.run("token")

The simplest way would be using mentions. This generates a list of users, for all users that were mentioned (#user) in a message. The advantage of this is that it easily handles multiple mentions. This works in the rewrite and async version of discord.py
#commands.command()
async def test(ctx):
users = ctx.message.mentions
await ctx.send(users[0].id)

Related

How to send a welcome message in server using discord.py in a server

I would like my bot to send a msg when someone joins the server (also mention them)
#client.event
async def on_member_join(member):
print('welcome!'f'{member}')
the only thing i want to change is this should be displayed in the general channel (its printing it in the terminal now) and that instead of only displaying the person's name i want it to mention them.
Thank you for your help!
to send a message to a channel you need to get the channel and then send the message to there, you can get the channel using get_channel
client.get_channel(1234567890)# change to your channel id
or using discord.utils.get incase you want to use name instead
discord.utils.get(member.guild.channels, name="welcome")
and for mention, discord.py has provided us with member.mention
await channel.send(member.mention)
and combining both things we can make this
#client.event
async def on_member_join(member):
channel = client.get_channel(1234567890) # change to your channel id
await channel.send(f"Welcome {member.mention}!")
print(f'{member.name} joined the server')

Input Messages in Discord.py

I'm trying to find a way to program my bot to clear a specific amount of messages in a channel. However, I do not know how to get my bot to run it's code based on the user's input data. For example, let's say I'm a user who wants to clear a specific amount of messages, like let's say 15 messages. I want my bot to then clear 15 messages exactly, no more, no less. How do I do that?
if message.content == "{clear":
await message.channel.send("Okay")
await message.channel.send("How many messages should I clear my dear sir?")
This is legit all I got lmao. I'm sorry that I'm such a disappointment to this community ;(
Using a on_message event, you'd have to use the startswith mehtod and create a amount variable which takes your message content without {clear as a value:
if message.content.startswith("{clear"):
amount = int(message.content[7:])
await message.channel.purge(limit=amount+1)
However, I don't recommend using on_message events to create commands. You could use the commands framework of discord.py. It will be much easier for you to create commands.
A quick example:
from discord.ext import commands
bot = commands.Bot(command_prefix='{')
#bot.event
async def on_ready():
print("Bot's ready to go")
#bot.command(name="clear")
async def clear(ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
bot.run("Your token")
ctx will allow you to access the message author, channel, guild, ... and will allow you to call methods such as send, ...

How do I make my discord py bot dm someone

So I made a bot that I needed for a server and I wanted to add a command called
!dm #(user) (message)
Any help please
Assuming you're using discord.py 1.x and discord.ext.commands:
import discord
from discord.ext import commands
... # Bot definition if this is the main file
#bot.command()
async def dm(ctx, user: discord.User, *, message):
await user.send(message)
This will need to be slightly adapted if your Bot instance variable is named something different or if it's in a cog.
Relevant documentation:
User.send()
Converters (how it gets a discord.User instance based on the type hint)
Keyword only arguments to avoid having to put quotes around the message

Make discord python rewrite bot tag message author

Am updating an old bot made with Discord.py and I can't get it to tag people as it used to do.
if 'test' in message.content:
msg = f'test sucessful {.author}'
await message.channel.send(msg)
On the discord rewrite documentation the only example I found was with {.author}, and it doesnt work. It does print the user's name, like person#1234, but it doesnt tag them.
There are different ways of tagging a user - they are equivalent:
Examples
await message.channel.send(f"Test successful, {message.author.mention}!")
await message.channel.send(f"Test successful, <#{message.author.id}>!")

How to get bot avatar link

It looks silly question but I want to embed bot avatar so how to do it. Like embed.set_thumbnail(url=user.avatar_url) I tried bot.avatar_url but it's not working.
Assuming you are using bot = discord.Client, Client does not have avatar_url as an attribute. Luckily though, Client does have a user attribute you can access, which means in your case you should be able to use bot.user.avatar_url.
Documentation here: http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.user
Client or i should say bot, itself dont have an attribute of avatar_url cuz its just a discord client, so you need to use bot.user.avatar_url instead
#bot.command()
async def botavatar(ctx):
BotAvatar = bot.user.avatar_url
embed = discord.Embed(
title=f'{bot.user.name}\'s Avatar:',
color=discord.Colour.red())
embed.set_thumbnail(url=f'{BotAvatar}')
await ctx.send(embed=embed)
Actually, Due to recent updates to discord.py, you can get the bot's avatar url from bot.user.avatar.url
So, in a command
#bot.command()
async def avatar(ctx):
await ctx.send("{}".format(self.bot.user.avatar.url))
Hope that helps!

Resources