discord python bot kick 命令
Posted
技术标签:
【中文标题】discord python bot kick 命令【英文标题】:discord python bot kick command 【发布时间】:2021-03-01 19:52:57 【问题描述】:我正在尝试制作一个可以使用命令 .kick 踢人的不和谐机器人 我让它显示一条消息,说(用户名)已从服务器中踢出,该消息仍然显示,但实际上并没有踢出他们。
这是我的代码:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
@client.event
async def on_ready():
print("Bot is Ready")
@client.command(aliases=['c'])
@commands.has_permissions(manage_messages = True)
async def clear(ctx,amount=2):
await ctx.channel.purge(limit = amount)
@client.command(aliases=['k'])
@commands.has_permissions(kick_members = True)
async def kick(ctx,member : discord.Member,*,reason= "I do not need a reason"):
await ctx.send(member.name + " has been kicked from the server, because "+reason)
await member.kick(reason=reason)
@client.command(aliases=['b'])
@commands.has_permissions(ban_members = True)
async def ban(ctx,member : discord.Member,*,reason= "I do not need a reason"):
await ctx.send(member.name + " has been banned from the server, because"+reason)
await member.ban(reason=reason)
错误信息说:
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\user\discordbot\bot.py", line 18, in kick
await member.kick(reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 512, in kick
await self.guild.kick(self, reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\guild.py", line 1849, in kick
await self._state.http.kick(user.id, self.id, reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 241, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
上述异常是以下异常的直接原因:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
请注意我的 .clear 命令运行良好,它只是踢和禁止
请帮忙,我在 python 3.9.0
【问题讨论】:
您的机器人可能没有权限踢用户。如果您将await ctx.send()
与await member.ban
切换,则不会打印该消息。您可以尝试将您的机器人角色移至顶部。
omg 非常感谢你,你不知道我有多开心
所以我所做的就是授予它每一个权限,并将角色设置为高于其他所有角色,并且它起作用了!
【参考方案1】:
@client.command()
@commands.has_permissions(administrator=True)
async def kick(ctx, member: discord.Member):
await member.kick()
await ctx.message.add_reaction("reaction")
await ctx.send(f"member.name has been kicked by ctx.author.name!")
await log_channel.send(f"ctx.author.name has kicked member.display_name")
这是我用于我的不和谐机器人的。它只允许具有 kick 权限的人使用此命令。我希望这会有所帮助。
【讨论】:
【参考方案2】:当发出此命令的用户或您的机器人未获得此类管理员权限时,就会出现此类错误。在您的服务器中,您只需创建一个角色为您的机器人提供所有权限,或者如果它由您管理,则可以将所有权限授予您的机器人。
下面的链接可能会对您有所帮助 https://support.discord.com/hc/en-us/articles/206029707-How-do-I-set-up-Permissions-
【讨论】:
以上是关于discord python bot kick 命令的主要内容,如果未能解决你的问题,请参考以下文章
基于 Python 的 Discord Bot 在本地工作,但从未在 heroku 上托管
如何使用 discord.js 从 discord bot 向特定用户发送消息