Member invites in discord.py - discord.py

#client.command()
async def invites(ctx, *, member: discord.Member=None):
if member == None:
member = ctx.author
totalInvites = 0
for i in await member.guild.invites():
if i.inviter == member:
totalInvites += i.uses
Invites = discord.Embed(description=f"{member.name}'s **Invites**\nInvites: {totalInvites}\nLeft: 0\nTotal: {totalInvites}", color=0x2F3136)
await ctx.send(embed=Invites)
So basically I'm creating an "invites" command that lets you see your invites. I want to show how many users left the guild by using an invite.delete. How can I call invite.delete to show how many invites have been deleted(how many users left from an invite) You can see in the photo how I have 297 invites but actually 100 of them left the guild but i have no way of showing that. Image
#client.listen("on_member_remove")
async def member_remove(member):
for i in member.guild.invites:
if i.inviter.id == member.id:
await invite.delete(reason="User left the guild") # how can i see users delete invites

Related

How to the list of people that has a certain role

I searched for it, but didn't find anything that could really help. Basically, I would like that when I run .highrank in a discord channel, the bots gives me a list of the people that have this role.
This is the current code I have:
#client.command()
async def highrank(ctx):
role =
await ctx.send(role.members)
I do not know how to make sure that this will give me a list of the people with the high rank role in the server.
Edit: I found this, but I only get the bot name and ID when I do the command, and only if the bot has the role.
#client.command(pass_context=True)
async def members(ctx, *args):
server = ctx.message.guild
role_name = (' '.join(args))
role_id = server.roles[0]
for role in server.roles:
if role_name == role.name:
role_id = role
break
else:
await ctx.send("Role doesn't exist")
return
for member in server.members:
if role_id in member.roles:
await ctx.send(f"{member.display_name} - {member.id}")
#client.command()
async def userrole(ctx, role: discord.Role):
# this will give the length of the users in role in an embed
members_with_role = []
for member in role.members:
members_with_role.append(member.name)
embed = discord.Embed(title=f"**Users in {role.name}: **{len(members_with_role)}", description="\n ".join(member.mention for member in role.members)
await ctx.send(embed=embed)
To get a role by its ID:
ctx.guild.get_role(123456789)
You would replace 123456789 with the role id you get by right-clicking the role (with Developer Mode enabled in Discord).
#client.command()
async def highrank(ctx):
HIGHRANK_ROLE_ID = 123456789 # replace this with your role ID
role = ctx.guild.get_role(HIGHRANK_ROLE_ID)
await ctx.send(role.members)
Alternatively, you can use ctx.guild.roles and locate the role by name using a helper such as discord.utils.find.
#client.command()
async def highrank(ctx):
# replace the role name with your "High Rank" role's name
role = discord.utils.find(lambda m: m.name == 'High Rank', channel.guild.roles)
await ctx.send(role.members)

Discord py delete voicechannel if its empty

Well im trying to create a voice channel by command so i can join it.
And as soon i leave the channel its supposed to get deleted.
What i tried:
if message.content.startswith('/voice'):
guild = message.guild
voicechannel = await guild.create_voice_channel(f"{str(message.author)}")
await asyncio.sleep(5)
while True:
if len(voicechannel.voice_states) == 0:
await voicechannel.delete()
break
doesnt even work tho
I believe that your issue is that you are stuck in an infinite while True loop. I would first suggest that you switch to using discord.ext.commands, it simplifies the process of creating a Discord Bot by a great deal. If you were using discord.ext.commands.Bot, you would be able to use the wait_for() function to wait until the voice channel is empty.
from discord.ext.commands import Bot
client = Bot(command_prefix="/")
#client.command(name="voice")
async def voice(ctx):
guild = ctx.guild
voice_channel = await guild.create_voice_channel(f"{str(ctx.author)}")
def check(member, before, after):
return member == ctx.author and before.channel == voice_channel and after.channel is None
await client.wait_for("voice_state_update", check=check)
await voice_channel.delete()
client.run("TOKEN")
The only issue that I think could happen, is if the member never joins the voice channel. To counter this, we can set up another wait_for() function to determine if the member ever joins before waiting for the member to leave the voice channel.
from asyncio import TimeoutError
#client.command(name="voice")
async def voice(ctx):
guild = ctx.guild
voice_channel = await guild.create_voice_channel(f"{str(ctx.author)}")
def check(member, before, after):
return member == ctx.author and before.channel is None and after.channel == voice_channel
try:
await client.wait_for("voice_state_update", check=check, timeout=60)
except TimeoutError:
await voice_channel.delete()
return

How do I make my discord.py bot recognise it's being pinged

I'm trying to make a discord bot via discord.py and want to create a command that shows the member's pfp. But, I want the bot to recognise when it is being pinged and it's pfp is being requested. How can I reformat this to make my bot recognise it's being refrenced or not as the member pinged (assuming the bots name and id is "Bot#1111")
Refrence command that I want the discord user to input
,membget #Bot#1111
#client.command()
async def membget(ctx, member: Member = None):
if not member:
member = ctx.author
if member == "Bot#1111":
print("1")
await ctx.send('This is me!')
return
await ctx.send(member.avatar_url)
await ctx.send('This here is a user!')
print(member)
Instead of if member == "Bot#1111": you can use if member == bot.user:

How to mention a user without putting id after command in server

#client.command()
async def status(current, content: str):
status = None
for member in current.guild.members:
if str(member.id) == content:
status = member.status
break
if status == discord.Status.online:
online_embed = discord.Embed(
title="awa is online", description="", color=0xF6E5E5
)
await current.send(embed=online_embed)
I would have to do #status [user id] every time, is there a way to make it #status only?
One of the simplest ways to get a member object in a command, allowing you to use a user's id, their name, etc., would be to use the converter they provide.
#client.command()
async def test(current, member: discord.Member=None):
if member == None: # if no member is provided..
member = current.author # ..make member the author
await current.send(f"Hello {member.mention}!")
Other questions like this:
Discord.py get user object from id/tag
Discord.py: How do I get a member object from a user object?

I've got a problem where I want to create a voice channel every time 2 people use the same dedicated command, which I managed to do but

the problem is I can't connect the 2 users to it because I don't have the ID of the channel that has been created
Look at this code
async def join(ctx,i=[0],c=[0]):
author = ctx.author
guild = ctx.guild
if i[0] == 0:
channel = await guild.create_voice_channel(f"""Voice chat # {c[0]}""")
i[0] = 0
c[0]+=1
i[0] += 1
if i[0] == 2:
i[0] = 0
print(i[0])
return i[0]
Could anyone potentially help me with this?
For some elaboration - What happens here is, if the user types !join It will create a voice channel, in which i want the user to be sent into immediately, then a different user also uses !join and gets sent into the same channel (which I also covered by counting the number of people joining), so essentially if 2 people use the command, I want them to be sent into a dedicated voice channel, if other 2 users do the same, they also get sent into their own dedicated voice chat on the server. Thanks <3
If a user isn't connected to a voice channel, you can't make him join one.
The only thing you can do is moving someone to another channel with the move_to function.
To track which user is connected to a channel, you could have a dict which takes a channel id as a key and the two users as a value:
channels = {}
async def join(ctx, i=[0], c=[0]):
author = ctx.author
guild = ctx.guild
if i[0] == 0:
channel = await guild.create_voice_channel(f"""Voice chat # {c[0]}""")
channels[channel.id] = [#Your two users]
i[0] = 0
c[0]+=1
i[0] += 1
if i[0] == 2:
i[0] = 0
print(i[0])
return i[0]
A example of how to use move_to:
#commands.command()
async def move(ctx):
channel = bot.get_channel(ID) #ID must be an INT
await ctx.author.move_to(channel)
If you want to change the channel's user limit:
channel = await guild.create_voice_channel(f"""Voice chat # {c[0]}""")
await channel.edit(user_limit=2)
If you want to check how many members are connected:
async def check_channels(ctx):
for channel_id in self.channels.keys():
channel = bot.get_channel(int(channel_id))
if len(channel.members) == 0:
await channel.delete()

Resources