我正在尝试为服务器创建afk命令,每当我在cmd中运行它时,都会出现此错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: AttributeError: 'Client' object has no attribute
'change_nickname'
我不确定为什么。谁能对discord.py有所了解的人可以帮助我吗? 到目前为止,这是我的代码:
import os
import discord
import asyncio
from discord.ext import commands
client = discord.Client()
bot_prefix="!"
bot = commands.Bot(command_prefix=bot_prefix)
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(f"Hi {member.name}, welcome to the chiaravalle discord server, please read the rules")
@bot.command(pass_context=True)
async def yeet(ctx):
await ctx.send("{0.author.mention} hit a yeet!".format(ctx.message))
@bot.command(pass_context=True)
async def ping(ctx):
await ctx.send("Pong!")
@bot.command(pass_context=True)
async def afk(ctx, mins):
await ctx.send("{0.author.mention} has gone afk for {1} minutes.".format(ctx, mins))
await client.change_nickname(ctx.author.mention, "{0.author.mention} [AFK]".format(ctx))
counter = 0
while counter != mins:
counter += 1
await asyncio.sleep(60)
if counter == mins:
await ctx.send("{0.author.mention} is no longer AFK".format(ctx))
bot.run("my token")
client.run("my token")
答案 0 :(得分:0)
您目前遇到的问题是,您必须以其他方式更改某人的昵称。您可以使用Member.edit()编辑某人的信息。
下面是您发送的代码的有效示例,它也将昵称重设为不带[AFK]的位置。
@bot.command()
async def afk(ctx, mins):
current_nick = ctx.author.nick
await ctx.send(f"{ctx.author.mention} has gone afk for {mins} minutes.")
await ctx.author.edit(nick=f"{ctx.author.name} [AFK]")
counter = 0
while counter <= int(mins):
counter += 1
await asyncio.sleep(60)
if counter == int(mins):
await ctx.author.edit(nick=current_nick)
await ctx.send(f"{ctx.author.mention} is no longer AFK")
break
答案 1 :(得分:0)
@bot.command()
async def afk(ctx, reason=None):
current_nick = ctx.author.nick
await ctx.send(f"{ctx.author.mention} is afk: {reason} ")
await ctx.author.edit(nick=f"[AFK] {ctx.author.name}")
counter = 0
while counter <= int(mins):
counter += 1
await asyncio.sleep(60)
if counter == int(mins):
await ctx.author.edit(nick=current_nick)
await ctx.send(f"{ctx.author.mention} is no longer AFK")
break
当我测试你的命令时,它说:
@2vw has gone afk for AFK minutes
如果原因只是: !afk afk