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

查看:34
本文介绍了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 是一个协程,所以你必须 await 它.您也没有真正正确地使用 commands .您应该为每个命令定义单独的协程并使用 client.command 装饰器来装饰它们.(你也不需要Clientcommands.Botdiscord.Client的子类)

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, *,nickname) 表示我们接受类似的命令

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天全站免登陆