被接受的答案中的提示可能无法正常工作:
discord.py库的rewrite版本与旧版本存在兼容性问题,旧版本仍在使用且未过时或弃用。
机器人应该检查自己的权限,以排除错误的原因之一。
如果出现错误,或者机器人本身的权限无效,则机器人应该说些什么,对吧?
需要实现某些内容以防止机器人尝试在DM或群组上下文中运行此命令。这几乎总是会出错。
我提议以下解决方案适用于pre-rewrite(假设您使用命令扩展):
import discord
from discord.ext import commands
import time
@bot.command(pass_context=True,description="Kicks the given member. Please ensure both the bot and the command invoker have the permission 'Kick Members' before running this command.")
async def kick(ctx, target:discord.Member):
"""(GUILD ONLY) Boot someone outta the server. See 's!kick' for more."""
if not str(ctx.message.channel).startswith("Direct Message with "):
msg=await bot.say("Checking perms...")
time.sleep(0.5)
if ctx.message.server.me.server_permissions.kick_members:
if ctx.message.author.server_permissions.kick_members:
await bot.edit_message(msg,new_content="All permissions valid, checking issues with target...")
time.sleep(0.5)
if target==ctx.message.server.owner:
await bot.edit_message(msg, new_content="All permissions are correct, but you're attempting to kick the server owner, whom you can't kick no matter how hard you try. Whoops!")
else:
if target==ctx.message.server.me:
await bot.edit_message(msg, new_content="Whoops! All permissions are corrent, but you just tried to make me kick myself, which is not possible. Perhaps you meant someone else, not poor me?")
else:
await bot.edit_message(msg, new_content="All permissions correct, and no issues with target being self or server owner, attempting to kick.")
time.sleep(2)
try:
await bot.kick(target)
await bot.edit_message(msg, ":boom: BAM! ***kicc'd***")
except Exception:
await bot.edit_message(msg, new_content="I was unable to kick the passed member. The member may have a higher role than me, I may have crashed into a rate-limit, or an unknown error may have occured. In that case, try again.")
else:
await bot.edit_message(msg, new_content="I've the correct permissions, {}, but you do not. Perhaps ask for them?").format(ctx.message.author.mention)
else:
await bot.edit_message(msg, new_content="I'm just a poor bot with no permissions. Could you kindly grant me the permission `Kick Members`? Thanks! :slight_smile:")
else:
await bot.say("'Tis a DM! This command is for servers only... try this again in a server maybe? :slight_smile:")
Member
is a subclass ofUser
, so you should be able to just doauthor.id
- Patrick HaughFile "C:\Users\Administrator\Desktop\dicordBot.py", line 25, in clear if ctx.message.author.server_permissions.administrator or ctx.message.author.id('344812358868402176'): TypeError: 'str' object is not callable
。 - user9123author.id
will return the id. You should doauthor.id == '123...'
- Patrick Haugh