【问题标题】:discord.py member.edit(nick=nickname) not workingdiscord.py member.edit(昵称=昵称)不起作用
【发布时间】: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


【解决方案1】:

这有帮助吗?

client.command(pass_context=True)
@commands.has_permissions(change_nickname=True)
async def hnick(ctx, member: discord.Member, nick):
    await member.edit(nick=nick)
    await ctx.send(f'Nickname was changed for {member.mention} ')

【讨论】:

  • client.comammnd(pass_context=True) 有什么作用?我以前见过它,但我认为它是 discord.ext 的旧语法。我的所有其他代码都可以在没有client.comammnd(pass_context=True) 的情况下使用 discord.ext(例如禁止和踢用户)
  • pass_context 已过时。它在v1.0 migration 期间被删除
  • 所以它不能解决我的问题?
【解决方案2】:

感谢@Mr_Spaar 的帮助。 解决办法:

@bot.command()
@commands.has_permissions(change_nickname=True)
async def nick(ctx, *, nickname):
    member = ctx.author
    await member.edit(nick=nickname)
    await ctx.send(f"Nickname was changed to {member.mention}.")

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2020-04-07
    • 2021-05-24
    • 2021-06-10
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多