简体   繁体   English

Discord.py Bot Kick/DM 命令

[英]Discord.py Bot Kick/DM Command

I'm learning how to create a discord bot using python and I'm having trouble with this one command.我正在学习如何使用 python 创建一个不和谐的机器人,但我在使用这个命令时遇到了问题。 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.我特别遇到的问题是如何踢特定用户(使用用户 ID),然后 DM 该用户。

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.这样做的目的是,如果某个适当角色类型的人!kickjohn一个特定的不和谐用户 ID ( john ) 被踢出,并且机器人会自动向服务器发送邀请。

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我认为你应该使用一个命令来使它更容易,如果你有一个on_message函数添加await bot.process_commands(message)像这样

@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 

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>")
@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)")

here is a working and perfect one for you这是一个适合你的工作和完美的

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!", case_insensitive=True)
token = "XXXXyyyyZzZz:AAAAAbbbbbCCCCCddddd"

@bot.command()
async def kick(ctx, member:discord.Member):
    await ctx.send(f"{member.mention} has been kicked from this server by {ctx.author.mention}")
    dm = await member.create_dm()
    server_name = "name of your server"
    invite_link = "put_link_here"
    await member.kick()
    await dm.send(f"You got kicked out of the server {server_name},\nYou can join again - {invite_link}")

bot.run(token)

Now when you will use - !kick @jhon the bot will kick jhon and send him a dm with an invite link .... 现在,当您使用 - !kick @jhon ,机器人将!kick @jhon并向他发送带有邀请链接的 dm ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM