Discord.py用户昵称更改 [英] Discord.py User nickname changing

查看:161
本文介绍了Discord.py用户昵称更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试让自己成为 ArmA 3 单元的机器人,这样做之后,我试图创建一个 Enlisting 命令,它将用户在服务器中的现有昵称更改为他们使用的昵称(他们的 ArmA 士兵姓名)。但是我在弄清楚如何做到这一点上遇到了麻烦。我将下面的代码留给您查看,并希望找到解决方案:

I have been trying to make myself a bot for my ArmA 3 unit, and upon doing so I have tried to create an Enlisting command, which changes the users existing Nickname within the server to the one that they enlist with (Their ArmA soldier name). But I am having some trouble figuring out how to do this. I'll leave my code down below for you to look at and hopefully figure out a solution :

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

Client = discord.Client()
client = commands.Bot(command_prefix = "+.")

@client.event
async def on_ready():
    print("ToLate Special Operations Reporting For Duty")
    await client.change_presence(game=discord.Game(name="By Slay > $enlist", type=3))
    print("For more information: Please contact Slay on twitter @OverflowEIP")

@client.event
async def on_message(message):
    if message.content.upper().startswith('+.ENLIST'):
        client.change_nickname(message.content.replace('changeNick', ''))

client.run('token')


推荐答案

change_nickname 是一个协程,因此您必须等待。您也没有真正正确地使用命令。您应该为每个命令定义独立的协程,并使用 client.command 装饰器对其进行装饰。 (您也不需要 Client 命令。Bot discord的子类。客户端

change_nickname is a coroutine, so you have to await it. You're also not really using commands properly. You should be defining separate coroutines for each command and decorating them with the client.command decorator. (You also don't need Client, commands.Bot is a subclass of discord.Client)

from discord.ext.commands import Bot
from discord.utils import get

client = commands.Bot(command_prefix = "+.")

@client.event
async def on_ready():
    print("ToLate Special Operations Reporting For Duty")
    await client.change_presence(game=discord.Game(name="By Slay > $enlist", type=3))
    print("For more information: Please contact Slay on twitter @OverflowEIP")

@client.command(pass_context=True)
async def enlist(ctx, *, nickname):
    await client.change_nickname(ctx.message.author, nickname)
    role = get(ctx.message.server.roles, name='ARMA_ROLE') # Replace ARMA_ROLE as appropriate
    if role: # If get could find the role
        await client.add_role(ctx.message.author, role)


client.run('token')

enlist(ctx,*,昵称)表示我们接受这样的命令

enlist(ctx, *, nickname) means we accept commands like

+.enlist apple
+.enlist bag cat
+.enlist "dog eat"

并为这些用户(调用命令的人)分配昵称

and will assign those users (the person who called the command) the nicknames

apple
bag cat
"dog eat"

这篇关于Discord.py用户昵称更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆