I'm new to discord.py and I'm a intermediate student, I made a program to give a role to a specific user id, but I encounter some problem that I cannot solve it, anyone can help?
#tasks.loop(seconds=5)
async def change_status():
for number in range(len(data_dict["username"]) - 1):
if data_dict["year_start"][number] == year_now and data_dict["month_start"][number] == month_now and data_dict["date_start"][number] == date_now:
userid = int(data_dict["username"][number])
member = Guild.get_member(userid)
role = Guild.get_role(840609566093606953)
member.add_role(role)
Have you set the guild object correctly,
You need the line:
Guild = client.get_guild(ID)
You can find the guild Id by enabling dev mode in user settings and then right clicking on the server name when your server is open
Related
I'm currently using this code to assign prefix to nickname if users has specific role:
#client.event
async def on_member_update(before, after):
role = discord.utils.get(before.guild.roles, name="FK")
if after.nick is not None and after.nick.startswith("F |"):
return
if after in role.members:
await after.edit(nick="FK | " + after.display_name, reason=None)
How can i remove (or reset the nickname) if the the role has been removed from the user?
I tried using if after not in role.members but it's not working, so i guess i'm not doing the right thing.
So I’m making a discord bot that creates a profile for a user based off of their username and userID. The userID is the unique identifier for the profile, and the username is what is responded and searched when looking for said profile.
In order to get the userID and username, I’ve been trying to use discord.User, and getting the userID then transferring that to a username, both return with this error:
error image
I can’t figure out how to fix this, I’ve tried many different ways of getting the username and ID, but they all return a similar error.
Packages:
Discord, discord.ext, replit, os, db(replit extension)
Language:
Python V 3.8.0
Code:
#bot.command()
async def create(ctx, message):
user_id = message.author.id
userName = bot.get_user(user_id)
Just use ctx - you don't need message. Also, if you just want the username and ID of the person who calls the command, you don't even need to use discord.User.
#bot.command()
async def create(ctx):
user_id = ctx.author.id
user_name = ctx.author.name
So I have been trying to create a command where I can run the command "-roster Arizona Cardinals" as an example. And what it will do is return a list of mentions, and name#discriminators of the members within the Arizona Cardinals Role. But the Issue I have been stuck with, is When I am trying to return the Franchise Owner, General Manager, and Head Coach User mentions within the cardinals role, I cannot seem to do it without breaking the for loop for the for member in role.members Here is a snippet of where I am attempting to troubleshoot.
#commands.Command()
async def roster(ctx, role: discord.Role=None):
em = discord.Embed(title=f"{role.name}\'s Current Team Roster:", color=role.color)
em.set_author()
fo=discord.utils.get(ctx.message.guild.roles,id=846618354478481409)
gm=discord.utils.get(ctx.message.guild.roles,id=846618640957833259)
hc=discord.utils.get(ctx.message.guild.roles,id=846618777213730837)
for member in role.members:
embed.add_field(name="\u200b",
value=f"{member.mention}\n"
f"`{member.name}#{member.discriminator}`"
)
Wait, I think if I just add a item loop (e.g member = [member for member in role.members]) and then the franchise owner general manager and head coach user mentions. then finish the loop with:
embed.add_field(name="\u200b", value=f"{member.mention}\n {member.name}#{member.discriminator}")
that should work if I am correct? Am I not? So my finishing snippet would be:
#commands.Command()
async def roster(ctx, role: discord.Role=None):
em = discord.Embed(title=f"{role.name}\'s Current Team Roster:", color=role.color)
em.set_author()
fo=discord.utils.get(ctx.message.guild.roles,id=846618354478481409)
gm=discord.utils.get(ctx.message.guild.roles,id=846618640957833259)
hc=discord.utils.get(ctx.message.guild.roles,id=846618777213730837)
for role in ctx.guild.roles:
member = [member for member in role.members]
if role == fo:
em.add_field(name="**__Franchise Owner__**:", value=f"{member.mention}\n"
f"`{member.name}#{member.discriminator}`"
)
elif role == gm:
. . .
else:
em.add_field(name="\u200b", value=f"{member.mention}\n"
f"`{member.name}#{member.discriminator}`"
)
So I'm working on a discord bot using discord.py and I'm trying to create a bot for the moderation team in a server, the bot will swap the 'Moderator' role with a 'Leave of absence' role for when they're not active, however the code I have come up with has a slight loopholing problem that I just can't figure out, the code for the commands is this
...
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
...
as you can see anyone could use the second command to just give themselves a moderator role, I can't set the second command to be moderator only use as the moderator will no longer have said role from using the first command, I'm racking my brain to think of a work around i.e. logging the command users id to a whitelist and having only those whitelisted id's be able to use the second command, I've done many googlesearches for this but have come back with no results, any suggestions would be appreciated, please forgive that this question is a bit lengthy and I'm still very new to coding in general so any help at all, even if you don't fully understand what I'm blabbering on about would be very appreciated, thank you.
Check for the loa role in the command (ex):
mod = None
for role in ctx.author.roles:
if role.id == 848032714715561985: mod = True
if mod:
#your code here
So from your question, I'm guessing that you would like to code basically a "storage" file and make sure the person on the leave of absence was previously a moderator.
What you could do is create a csv file, for example records.csv (in the same folder as your main .py file of course), and every time someone calls the sl command, the program will record the user that used it.
import csv
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
file = open("records.csv", "w")
file.writelines(str(ctx.author.id))
file.close()
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
found = False
with open('records.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == str(ctx.author.id):
found = True
break
if found = False:
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
else:
await ctx.send("We do not have history of you having a moderator role.")
Are you running the program through an online environment like Repl.it? If so, this may not be the best way to approach this problem since people would have access to your this records.csv file (which you may not care about but just in case). If you are running the program through your desktop file directories, then there should be no privacy concerns.
I'm trying to do a bot that picks up all roles from user, send a message that mention all roles in a channel, so the bot need remove all roles and add a 'Prisoner' role and send a reason for the prison. How do I do this?
I'm trying to do this command for 3 days, but nobody could help me.
You can try this for deleting all roles and adding prisoner role:
#client.command()
async def prison(ctx, member: discord.Member):
member_roles = []
for role in member.roles:
member_roles.append(role)
await member.remove_roles(role)
member_roles = ', '.join(member_roles)
await ctx.send(f'{member.mention} is in prison. His {member_roles} roles are deleted.')
prisoner = discord.utils.get(ctx.guild.roles, name='Prisoner')
await member.add_roles(prisoner)
After you create the Prisoner role, change the permissions of this role for all the channels except for Prison channel in Discord.
Note: There could be syntax problems in my code because I'm on mobile right now. If any problem raises, just comment.