[英]Discord.py Bot Kick/DM Command
我正在學習如何使用 python 創建一個不和諧的機器人,但我在使用這個命令時遇到了問題。 我想要做的是踢一個特定的用戶,然后使用機器人將他們發送回不和諧服務器的邀請。 這是一個愚蠢的想法,但我真的想讓它發揮作用。
我特別遇到的問題是如何踢特定用戶(使用用戶 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
) 被踢出,並且機器人會自動向服務器發送邀請。
我認為你應該使用一個命令來使它更容易,如果你有一個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
只需將所有 <> 替換為您想要的內容
@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)")
這是一個適合你的工作和完美的
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)
!kick @jhon
,機器人將!kick @jhon
並向他發送帶有邀請鏈接的 dm ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.