Search code examples
pythondiscord.pymysql-connector-python

Convert discord username to member object


I have the following script,

async def kick(ctx, u: discord.User):
    await client.kick(u)

It does have pass_context=True but that is in the primary function.

This script works but the problem is that it is passing a string with the author id (example: johnwayne#345) that has been pulled from a database while the kick command is expecting a member object.

How can I use this to look up a member object?


Solution

  • Are you sure u is just a string? I just checked and for me it's passing a User object.

    However, where I think the problem lies is that you're not calling kick correctly. You want to call Guild.kick or Member.kick. I'm assuming client is your actual botclient and that does not have a kick command.

    EDIT:

    So assuming the kick command receives the id of a member as a string every time you call it, you can use the following. You won't actually have to pass u: discord.User since it's only passing a string.

    @client.command()   
    async def kick(ctx, id):
        await ctx.guild.kick(ctx.guild.get_member(int(id)))
    

    Since you mentioned in your question that you save their id in a database but then gave the example of their name I'll include the following in case you're saving their names and not the actual User.id.

    @botclient.command()   
    async def kick(ctx, name):
        await ctx.guild.kick(ctx.guild.get_member_named(name))