【发布时间】:2021-05-15 19:30:11
【问题描述】:
我正在使用 discord.ext 制作一个 Discord 机器人。我想更改命令nick 上的用户昵称。
为此,我编写了以下代码:
from discord.ext import commands
import discord
# set prefix as "w!"
bot = commands.Bot(command_prefix="w!")
# do stuff when certain error occurs
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("Error: Bot missing permissions.")
if isinstance(error, commands.CommandNotFound):
await ctx.send("Error: The command was not found")
if isinstance(error, commands.MissingPermissions):
await ctx.send("Error: You don't have permission to do that.")
@bot.command()
# check if the user has the permission to change their name
@commands.has_permissions(change_nickname=True)
async def nick(ctx, member: discord.Member, *, nickname):
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
# reset the nickname if no new name was given
@nick.error
async def nick_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
member = ctx.author
await member.edit(nick=None)
await ctx.send(f"Reset nickname to {member.mention}.")
bot.run("TOKEN")
当我在 Discord 中输入 w!nick test name 时,它不会回复消息,也不会更改我的昵称。但是当我输入w!nick 时,它会重置我的昵称。
【问题讨论】:
-
你提到用户还是输入他的名字?
-
你应该只能更改自己的名字;如果这就是你所说的。 @Mr_Spaar
-
在这种情况下,为什么会有
member参数? -
哦...你说得对,这实际上是我的错误。
标签: python discord discord.py