0
votes

I'm learning how to create a discord bot using python and I'm having trouble with this one command. What I'm trying to do is kick a specific user and then dm them a invite back to the discord server using the bot. It is a silly idea but I really want to make it work.

I specifically am having trouble with is how to kick a specific user (with user ID) and then DM that user.

Thanks!

Here the code:

if message.content == '!kickjohn':
    if "527290609166188554" in [role.id for role in message.author.roles]:
        <KICK JOHN COMMAND>
        await client.send_message(message.channel, "_**Bye Bye John!**_")
        await client.send_message(<JOHN>, 'https://discord.gg/XXXXXXX')
    else:
        await client.send_message(message.channel, "sorry you can't do that")

The goal of this is that if someone of the appropriate role types !kickjohn a specific discord user id (john) gets kicked and the bot automatically dm's john an invite to the server.

3

3 Answers

0
votes

I think you should use a command to make it easier, if you have an on_message function add await bot.process_commands(message) like so

@bot.event
async def on_message(message):
    await bot.process_commands(message)
@commands.has_role("role_name_here")#makes it so that only works with specific role
@bot.command(pass_context=True)
async def kick(msg,user:discord.Member): #this converts the member you mention into a usuer object, you can also do it by user id or server nickname if you don't want to mention them
    """[Create an invite code then kicks the user]
    """
    code=await bot.create_invite(msg.message.channel) #create the invite code
    await bot.send_message(user,f'{code}') #Send the invite code first because you must have common server to send message to user
    await bot.kick(user) #kick the user 
0
votes

just replace all the <> with what you want to be in them

    @client.command(pass_context=True)
    async def kick(ctx, user: discord.Member):
        if "527290609166188554" in [role.id for role in ctx.message.author.roles]:
            await client.send_message(user, "<message><link>")
            await client.kick(user)
            await client.say("{} Just got Kicked".format(user.name))
        else:
            await client.say("<access denied because of improper role message>")
0
votes
@client.command(description="kicks a user with specific reason (only admins)") #kick
@commands.has_permissions(administrator=True)
async def kick (ctx, member:discord.User=None, reason =None):
 try:
    if (reason == None):
        await ctx.channel.send("You  have to specify a reason!")
        return
    if (member == ctx.message.author or member == None):
        await ctx.send("""You cannot kick yourself!""") 

    message = f"You have been kicked from {ctx.guild.name} for {reason}"
    await member.send(message)
    await ctx.guild.kick(member, reason=reason)
    print(member)
    print(reason)
    await ctx.channel.send(f"{member} is kicked!")
 except:
    await ctx.send(f"Error kicking user {member} (cannot kick owner or bot)")