Discord.py Rewrite Role Hierarchy Check - url-rewriting

I have written a bot in discord.py rewrite. So far, it only has basic options such as !kick, !ban, and !unban. However, it is currently set up that anyone with the proper permissions can do this to anyone. This means that someone with the "Moderator" rank can ban someone with the "Admin" rank, which should not be allowed. How do I add a simple check to see if the person's rank is above/below the person they are trying to kick/ban?

Member objects have a top_role attribute, and you can compare roles directly (higher roles are "larger"):
#bot.command()
async def ban(ctx, target: Member):
if target.top_role >= ctx.author.top_role:
await ctx.send("You can only ban people below you")
return
...

Related

Changing a member's nickname with JDA

I am trying to change someones nickname, but I keep getting this error: net.dv8tion.jda.api.exceptions.HierarchyException: Can't modify a member with higher or equal highest role than yourself!
This is my code:
Member m = e.getMember();
m.modifyNickname(rank.getDisplayName() + " | " + p.getName()).queue();
In the first case, whenever a bot tries to modify the:
Nickname
Role
Or tries to:
Kick
Ban
Timeout
a user, Discord will check if the user is allowed to do so.
This is checked by Discord looking if the bot has a role higher, then the highest role the user it tries to effect has..
In JDA, there should be a util for this. PermissionUtil.canInteract(Member, Member). However, as far as I could find. This is something JDA internal, and should not be used.
In your case, you are either above the bot, or you're the owner. (Whom is always the highest and always has admin)

How can i make a discord bot say something a user says without prefix

Im trying to figure out how to make a discord bot say something a specific user says. For example, if i say something in general the bot says what i said in the text channel. I want it to be user specific but i dont know how to make this in the bot. Thanks to whoever answers this.
You use the on_message(ctx) event. For example:
#client.event
async def on_message(ctx):
message = ctx.content.lower()
if 'hello' in message and ctx.author.id == MY_ID:
await ctx.channel.send('Hey there!')
await client.process_commands(ctx)
Note: await client.process_commands(ctx) is very important, without it, all your commands won't work.
MY_ID is your your Discord ID (an integer value), so the bot will only respond if the author's ID equals your ID.

Discord.py how to ban a user that isn't a member

user=get(ctx.guild.members, name="nameUser")
await ctx.guild.ban(user, reason=None)
This is the code I'm using, I'm helping a friend to create a new discord server, but he wants to have the same banned user on both of them. I'm trying to do that, but I only get that error: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'. Thank you in advance for any help.
User ID is the snowflake integer associated with the user, visit this support post to see how to find it
# Use fetch user if the account is not in a guild with the bot
user = await bot.fetch_user(user_id)
await ctx.guild.ban(user, reason="Testing", delete_message_days=0)

Discord.py I have problem with getting a list of banned users

for member in ctx.guild.users:
print(str(member.name))
print(str(await is_banned(ctx.guild, member))+"\n\n")
async def is_banned(guild, user):
try:
entry = await guild.fetch_ban(user)
except discord.NotFound:
return False
return True
This is the code I'm using, but I get only the users that are not banned. Can you help me?
The reason why it doesnt show people who are banned. Is that ctx.guild.members only shows persons who are in the server. Thus excluding those who are banned. As those people arent in the server anymore.
This results in a list with people who arent banned.
If you want to get a list of banned people you need to do await ctx.guild.bans(). As explained in the documentation. This will return a list of banned users.

Discord.js Display Server User Count separately from Server Bot Member Count

I am trying to create a serverinfo command for my Discord.js Bot. I am trying to get the Discord Server User Count separate from the Server's Bot Count. I have been told to use the .filter but I don't understand how to filter the bot count from the user count.
Well from guild.members.cache you'll get a Collection with all the members (users and bots). With .filter you can "exclude" items of a collection if they don't match something. For example:
guild.members.cache.filter(member => !member.user.bot).size;
Should return the number of members that aren't bots on guild.
User filter:
client.guilds.get('Your guild id').members.cache.filter(member => !member.user.bot).size;

Resources