So I'm trying to make a bot that when a command is entered. It detects a users online status. I haven't started coding it yet because I really don't know how I would go about it. Would anyone mind helping me?
For a bit more documentation. I want the command to do the following;
Take in the command "status"
Check the mentioned users status
Say what the users status is.
Use the Member.status attribute of Member objects. It will be either a value of the discord.Status enumerated type, or a string.
from discord.ext.commands import Bot
from discord import Member
bot = Bot('!')
#bot.command(pass_context=True, name='status')
async def status(ctx, member: Member):
await bot.say(str(member.status))
bot.run('token')
for any future viewers this code is outdated, here's my updated version of course you can edit this to your desire
#client.command()
async def status(ctx, member : discord.Member=None):
if member is None:
member = ctx.author
embed=discord.Embed(title=f"{member.name} your current status is", description= f'{member.activities[0].name}', color=0xcd32a7)
await ctx.send(embed=embed)
Related
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"!)
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")
I need when I run a command like /insult #name the bot #'s the person in the argument of the command and sends an image. I can do most of the rest but I can't seem to figure out of to have it #mention the person.
To mention a user in a command, you can use member: discord.Member. This helps you get a member object in the command itself. You can view more on how you can use a discord.Member object here. An example on how to use this in a command can also be found in the docs, view this: Discord Converters.
You can view how these can be incorporated below, including a None variable as a default to avoid errors in your console if the ctx.author does not mention a member.
#client.command() # or bot.command(), or whatever you're using
async def insult(ctx, member:discord.Member=None):
if member == None: # Happens if ctx.author does not mention a member..
member = ctx.author # ..so by default the member will be ctx.author
# You can use member.mention to mention/ ping/ # the person assigned as member
await ctx.send(f"Be insulted {member.mention}!")
# A not as good way to do it would be:
await ctx.send(f"Be insulted <#{member.id}>!")
# both methods work the same way, but member.mention is recommended
How do I make a bot in Discord.py that will assign roles present in a role.json file, while using the same command to both remove and add the same role. For example, ?role <rolename> will both add and remove a role, depending on if the user has the role assigned. I'm a bit confused on how to achieve this.
My current bot uses ?roleadd <rolename> ?roleremove <rolename>.
I'm not sure where your role.json file comes into play, but here's how I would implement such a command
#bot.command(name="role")
async def _role(ctx, role: discord.Role):
if role in ctx.author.roles:
await ctx.author.remove_roles(role)
else:
await ctx.author.add_roles(role)
This uses the Role converter to automatically resolve the role object from its name, id, or mention.
This code basically just checks that if the command raiser is the owner of the server or not and then assigns the specified role to him.
#bot.command()
#commands.is_owner()
async def role(ctx, role:discord.Role):
"""Add a role to someone"""
user = ctx.message.mentions[0]
await user.add_roles(role)
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
Let me break the code down:
#bot.command()
#commands.is_owner()
These are just plain function decorators. They are pretty much self-explanatory. But still let me tell.
#bot.command() just defines that it is a command.
#commands.is_owner() checks that the person who has raised that command is the owner.
async def role(ctx, role:discord.Role): > This line defines the function.
user = ctx.message.mentions[0] #I don't know about this line.
await user.add_roles(role) #This line adds the roles to the user.
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
#It just sends a kind of notification that the role has been assigned
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.