discord.py | how to get list user banned name and id - discord.py

Discord.py
user banned by me, i want to see a list of banned user and id to remove the ban from user.
!banlist
- for made list of banned user name and id

In version 0.16.12 you can use client.get_bans(serverID) which returns a list of User objects. You can then iterate through the list and get the id and name from each user. If you wanted to have the bot list the banned users' names and ids, you could do something like:
bannnedUsers = await client.get_bans(serverID)
for user in bannedUsers:
await client.send_message(channelID, user.name + ' ' + user.id)
In the rewrite branch it's a little more complicated. You would use guild.bans() to get a list of tuples with each tuple containing a user object and a string with the reason for the ban. For the same result as before, you would do something like:
bans = await guild.bans()
for ban in bans:
await channel.send(ban[0].name + ' ' + ban[0].id)
It should be mentioned that the bot will need the ban_users permission to be able to access these coroutines.

Related

How do I add multiple roles form a list to a user?

I have a command that adds all the current roles of a user to a Database (MongoDB).
The code:
def add_roles_to_db(self):
check = cursor.find_one({"_id": self.ctx.author.id})
if check is None:
cursor.insert_one({"_id": self.ctx.author.id, "roles": [str(r) for r in self.ctx.author.roles[1:]]})
else:
cursor.update_one({"_id": self.ctx.author.id}, {"$set": {"roles": [str(r) for r in self.ctx.author.roles[1:]]}})
The code to get the roles:
def get_roles_from_db(self):
return cursor.find_one({"_id": self.ctx.author.id})["roles"]
When I get the roles from the DB I get a list, everything I've tried led to an error. Error: "AttributeError: 'str' object has no attribute 'id'"
if len(roles) != 0:
await author.add_roles(*roles)
I saw a other post where someone added roles via a list but that didn't work
You're passing a list of strings, not a list of Roles. Turn them into discord.Role instances using the id's first, and then pass them to add_roles.
You can get them using Guild.get_role, or Guild.roles.
await author.add_roles(*[discord.Object(role_id) for role_id in roles])
One good way of adding multiple roles to a user is to have the list of role IDs into a list. You would have to look at your code and figure out how to do that bit, as I don't know either but I reckon just append it into a list. Then interate through each item in that list (each ID) and append it.
Example code:
guild = client.get_guild(1234) #replace 1234 with your guild ID
guild = ctx.guild #this is another way of doing it, chose the one above or this
role_ids = [] #your role ids would be in this list
for id in role_ids:
role = guild.get_role(id)
await author.add_roles(role)
await ctx.send("Given you all the roles!")
I haven't tried this myself, but I don't see why it wouldn't work.
If you need any more clarification, please ask me and if this has worked, please mark it as correct! :)

i want auto role in jda, (when member join, add role by id)

i want when user join some discord guild, bot will add role by id to newcomer.
i have already use this code block, but that isn't work!
`
if (((GuildMemberJoinEvent) event).getGuild().getName().equals("something name")){ System.out.println(((GuildMemberJoinEvent) event).getMember().getUser().getName() +((GuildMemberJoinEvent) event).getGuild()); Role r = ((GuildMemberJoinEvent) event).getGuild().getRoleById("1019811149602107402"); ((GuildMemberJoinEvent) event).getMember().getGuild().addRoleToMember(((GuildMemberJoinEvent) event).getMember().getUser(),r); }
`
This is how to grant roles by id:
event.getGuild().addRoleToMember(member, event.getGuild().getRoleById("1034143551568752682")).queue();
Just put this in onGuildMemberJoin and check for guild name like you have here.
Also please make your code readable

Im trying to find if a member in the server has a specific role and then remove the role from him but i dont know how, here's the code:

#client.command()
async def find(ctx):
guild = ctx.message.guild
role = discord.utils.get(ctx.guild.roles,name="Palpatin")
for member in guild.members:
if role in member.roles :
await ctx.send(f'{member}')
await member.remove_roles(role)
When I try to use the code there are no errors but the bot doesn't send what member has the role either removes the role from that member
As #Weylyn said, your variable role might be None if there is no role named exactly Palpatin, if that is the case, your if will always be evaluated to False since None in any list will always be False, this might be why you do not get any message nor any error.
Also, your for might not work if you do not have the member intents enabled (check here https://discordpy.readthedocs.io/en/stable/intents.html )

discord.py Can I get a user ID using just the user's name?

discord.py, Can I get a user ID using just the user's name?
I want to know the syntax
utils.find? MemberConverter?
discord.Guild.get_member_named?
If you have a guild object then you can
member = discord.utils.get(guild.members, name="name")
member_id = member.id

Discord.js #everyone / #here issues with message

I have the code here for my embed announce feature, if a role is mentioned then it sends that role mention to the channel and then sends the rest of the message in an embed, i also have a variation for user mentions. How do i adapt this to do the same for #everyone & #here? they dont have ID's like roles. Either that or i cant find the ID of #everyone & #here. typing #everyone results in #everyone being returned, not an ID
if (args[1].startsWith('<#&') && args[1].endsWith('>')) {
message.channel.send(args[1])
const embed = new Discord.MessageEmbed()
.setTitle(`${(args.slice(2).join(" "))}`)
.setColor(0x320b52)
.setTimestamp()
.setFooter('Requested by ' + message.author.tag)
message.channel.send(embed);
Correct, #everyone and #here do not have IDs. Simply check whether either of them matches args[1].

Resources