Discord.py Bot Kick / DM命令

时间:2019-04-01 04:38:54

标签: python discord discord.py

我正在学习如何使用python创建Discord机器人,并且在使用此命令时遇到了麻烦。我要执行的操作是踢一个特定用户,然后使用该bot向他们发送回不和谐服务器的邀请。这是一个愚蠢的主意,但我真的想使它生效。

我特别麻烦的是如何踢一个特定用户(具有用户ID)然后向该用户DM。

谢谢!

代码在这里:

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

此操作的目标是,如果某个具有适当角色类型!kickjohn的人被踢出特定的不和谐用户ID(john),并且该漫游器自动dm约翰向服务器发出邀请。

3 个答案:

答案 0 :(得分:0)

我认为,如果您有一个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 

答案 1 :(得分:0)

只需将所有<>替换为您想要的内容

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

答案 2 :(得分:0)

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