#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?
Related
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)
#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
Here is my code:
#bot.command()
async def list(ctx):
guild = ctx.guild
members = '\n - '.join([member.name for member in guild.fetch_members])
await ctx.send(f'Guild Members:\n - {members}')
I have also tried guild.members but does not work, here is the error
The Command raised an exception: TypeError: 'method' object is not iterable. Please, use a valid command.
list is keyword in python, try not to use it for user-defined function.
Here is the code you can use to fetch members with serial number:
#bot.command()
async def memberlist(ctx):
members = ''
for index, member in enumerate(ctx.guild.members, start=1):
members += f'{index}) {member}\n'
await ctx.send(members)
So I'm making a user info command and when i run the command, it doesn't work and gives no error.
Heres my code:
#commands.command()
async def info(self, ctx, *, member: discord.Member):
embed=discord.Embed(color=0xFFFFF0, title=f"{ctx.author.name}'s Info", description="Displays user information.")
embed.set_thumbnail(url=f"{ctx.author.avatar_url}")
embed.add_field(name="User ID:", value=f"{ctx.author.id}", inline=True)
embed.add_field(name="Color:", value=f"{ctx.author.top_role.mention}\n[{ctx.author.top_role.colour}]")
embed.add_field(name="Join Date:", value=f"{ctx.author.joined_at}")
embed.timestamp = datetime.datetime.utcnow()
embed.set_footer(text=f"Requested by: {ctx.author}", icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
You basically put ctx.author, which the author is you, you cant show other user's info if you did that. You should use an if statement if you want to show other user's info.
And the last thing is the Joindate. it will send this weird time 04:22:23.699000. So you should convert the time to an strftime.
The Updated Code :
#commands.command()
async def info(self, ctx, *, member: discord.Member = None): #changed the member to None so it will work if the user didnt mention the member
if member == None: # shows that if the use didnt mention the member (if statement)
member = ctx.author
# changed all embed of the ctx.author to member
embed=discord.Embed(title=f"{member.name}'s Info", description="Displays user information.", color=0xFFFFF0)
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.add_field(name="User ID:", value=f"{member.id}", inline=True)
embed.add_field(name="Color:", value=f"{member.top_role.mention}\n[{member.top_role.colour}]")
embed.add_field(name="Join Date:", value= member.joined_at.strftime("%B %d %Y\n%H:%M:%S %p")) # make the time look nice
embed.timestamp = datetime.datetime.utcnow()
embed.set_footer(text=f"Requested by: {ctx.author}", icon_url=ctx.author.avatar_url) # changed member to ctx.author als
await ctx.send(embed=embed)
I'm trying to make a command where the bot sends me an invite of a server by ,dm (server id) and this is what I made so far:
#client.command(name='dm')
async def dm(ctx, guild_id: int):
if ctx.author.id == owner:
guild = client.get_guild(guild_id)
guildchannel = guild.system_channel
invitelink = await guildchannel.create_invite(max_uses=1,unique=True)
await ctx.author.send(invitelink)
Some servers work and some don't. The ones that don't work sends this: 'NoneType' object has no attribute 'create_invite' and sometimes it sends me 2 invite links of the same server. Please help as soon as possible and thanks in advance.
Your client.get_guild(guild_id) is returning None, this means your bot either isn't in the guild or your guild_id isn't a valid guild ID
UPDATE:
On second thought I think your target server does not have a system_channel. You can check then in your Guild Settings > Overview > System Messages Channel.
So I found a way to change this, here is the code:
#client.command(name='dm')
async def _dm(ctx, guild_id: int):
if ctx.author.id == owner:
guild = client.get_guild(guild_id)
channel = guild.channels[-1]
invitelink = await channel.create_invite(max_uses=1)
await ctx.author.send(invitelink)