Pythion Discord Bot #'ing people - python

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

Related

discord.py command can only work for user with one of several role in a list

I am currently building a discord bot using Discord py with one of its command can only be used by a user with one of several roles that I have set up.
Currently, I store all of the role name in a list since I also have a command to add new role to that list. This is a snippet of what I have right now
role_name = ["Moderator"]
#bot.command(name='role',help='adding role for staff moderating role (admin only command)')
#commands.has_permissions(administrator=True)
async def add_role(ctx, role):
if role not in role_name:
role_name.append(role)
print(*role_name) #use case 1: when run this command to add 'mine' it print "Moderator mine"
#bot.command(name='add',help='Add channel, when you run this command, you must be in the channel ')
#commands.has_any_role(role_name)
async def add_channel(ctx):
if ctx.channel.id not in channel_id:
channel_id.append(ctx.channel.id)
I have double check on my account's role on discord and it already have Moderator role
However, when I run the command to test - this is what i got
discord.ext.commands.errors.MissingAnyRole: You are missing at least one of the required roles: '['Moderator']'
Is there any way to do what I want?
/Edit: Add the adding role to the role_name for the sake of clarity. I also have tried ",".join(role_name) in has_any_role() however, I still get this issue discord.ext.commands.errors.MissingAnyRole: You are missing at least one of the required roles: ''
has_any_role() accepts *items and is looking for str or int objects. The list is read as a string '["Moderator"]'. You can check this by trying to rename your rolename as well.
A simple fix for this would be:
#commands.has_any_role(*role_name) for your bot from read the list as a string.
So it looks like this:
role_name = ["Moderator"]
#bot.command(name='add',help='Add channel, when you run this command, you must be in the channel ')
#commands.has_any_role(*role_name)
async def add_channel(ctx):
if ctx.channel.id not in channel_id:
channel_id.append(ctx.channel.id)
After some extra research regarding the decorator. Apparently, based on this answer https://stackoverflow.com/a/64544809/10467473 -
A decorator is invoked once as soon as the module is imported, and
then the function that gets called is whatever was provided by the
decorator definition.
Therefore, I tried a different approach to check the role like how I wanted it to be and now it works perfectly fine! This is the method that I use.
role_name=[]
#bot.command(name='role',help='adding role for staff moderating role (admin only command)')
#commands.has_permissions(administrator=True)
async def add_role(ctx, role):
if role not in role_name:
role_name.append(role)
#bot.command(name='add',help='Add channel, when you run this command, you must be in the channel ')
async def add_channel(ctx):
global role_name
user_role = ctx.author.roles
for rl in user_role:
if (rl.name in role_name):
if ctx.channel.id not in channel_id:
channel_id.append(ctx.channel.id)
break

How to add a role to a specific member when he joins a discord server

I'm new to python, so can someone help me with my code, because it is not working properly. When the user that I want joins it doesn't give it a role. I want to make it work for a specific user only to give a role when joining discord server.
#client.event
async def on_member_join(member):
member = get(member.id, id=member_id)
role = get(member.guild.roles, id=role_id)
await member.id(member)
await member.add_roles(role)
I don't even know why you're making this so complicated.
Since you already have the member as an "argument" you can work with it and don't have to define member again.
We can get the ID of member very easily with member.id. If we want to compare this with a real ID, we do the following:
if member.id = TheIDHere:
# Do what you want to do
The function for the role is correct, yet I cannot find a use for await member.id(member). What is the point of this/has it any use?
How you add the role at the end is also correct, but the code must be indented properly and best you work with an if / else statement, otherwise the bot will still give an error at the end in the console if always the wrong member joins.
The whole code:
#client.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, id=IDOfTheRole) # Get the role from member.guild and the id
if member.id == TheIDHere: # If the member.id matches
await member.add_roles(role) # Add the role for the specific user
else: # If it does not match
return # Do nothing
You may also need to enable the members Intent, here are some good posts on that:
https://discordpy.readthedocs.io/en/stable/intents.html
How do I get the discord.py intents to work?

Is there a way to give roles without a command?

I have a database with discord id, seconds and more
A plugin from a game server is measuring player activity and feed it inside the database
I was wondering if there is a way to make my bot give a role to that specific discord id as long as his activity is bigger than some value
ive tried this way but it doesn't work
#tasks.loop(seconds=10)
async def checkDB():
delete_oldDate()
list = get_db_list('players_activity', '*')
for i in list:
if str(i[3]) != 'None' and int(i[2]) >= min_activity:
user = i[3]
role = discord.utils.get(user.guild.roles, giveawayRole_ID)
await client.add_role(user, role)
else:
print("")
any ideas?
Adding roles inside and outside of a command works the same way. client.add_role doesn't exist, which is why it doesn't work. The correct function is Member.add_roles(list_of_roles).
Going off of context, I'm gonna assume your user is already a discord.Member instance, as you're calling it's guild attribute. You can also use Guild.get_role(role_id) to get the discord.Role instance of the role.
user = i[3]
role = user.guild.get_role(giveawayRole_ID)
await user.add_roles([role])
EDIT:
Apparently your user was not a discord.Member instance yet, so you'll have to get that first. You should've gotten an error from that though, which you didn't mention. In case user is a string, user.guild shouldn't work at all either.
First of all, instead of storing their Discord ID as Mihái#8090, you should store their actual ID, which is a number. You can get this by using Member.id whenever they use a command (to put them in the database), or right-clicking them and using Copy ID in Discord.
Seeing as you always want to use the same Guild, you should already have the Guild's ID stored somewhere as well.
guild_id = 000000000000 # Whatever the ID of your Guild is
guild = client.get_guild(guild_id)
member = guild.get_member(int(i[3]))
role = guild.get_role(giveawayRole_ID)
await member.add_roles([role])
So first get the Guild instance, then the user's Member instance in that guild, then the Role instance, and then give the member the role.

Profile command issue

I want to make a simple profile command. When there is no mention bot shows your profile, when there is mention, bot shows profile of mentioned user.
#!профиль - профиль
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
else:
return
When I am using command WITHOUT mention I get this:
discord.ext.commands.errors.MissingRequiredArgument: member is a required argument that is missing.
If you want to show your profile when the member parameter's empty, you can give a value to it. For example you can do async def профиль(ctx, member: discord.Member=None):. That means, if user doesn't fill the member parameter, it'll be None. Then in the code, you can add:
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
member = ctx.author
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
So if the member is None, then it'll be the ctx.author.

How would I detect user status? || Discord.py

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)

Categories

Resources