簡體   English   中英

Discord.py 將角色添加到成員

[英]Discord.py Add Role to member

我目前正在研究 discord.py 機器人,它將用於管理 discord 服務器上的許可證。 目前卡在成功兌換代碼后如何添加角色。 我在我的 discord 服務器中添加了 3 個角色(1 天、7 天、30 天),如何讓機器人為成功兌換許可證的用戶添加角色?

這是我現在的代碼:

import discord
import random
import re
from discord.ext import commands
from discord.utils import get

client = discord.Client()


l1d = open('licenses1d', 'r').read().splitlines()
l7d = open('licenses7d', 'r').read().splitlines()
l30d = open('licenses30d', 'r').read().splitlines()

def license_gen1d():
    a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
    license = ''

    while True:
        while len(license) < 30:
            character = random.choice(a)
            license += character
        if len(license) == 30:
            with open('licenses1d', 'a') as f:
                f.writelines(license + '\n')
                f.close()
            print('Successfuly Generated 1 day License: ' + license)
            return(license)

def license_gen7d():
    a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
    license = ''

    while True:
        while len(license) < 30:
            character = random.choice(a)
            license += character
        if len(license) == 30:
            with open('licenses7d', 'a') as f:
                f.write(license + '\n')
                f.close()
            print('Successfuly Generated 7 days License: ' + license)
            return(license)

def license_gen30d():
    a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
    license = ''

    while True:
        while len(license) < 30:
            character = random.choice(a)
            license += character
        if len(license) == 30:
            with open('licenses30d', 'a') as f:
                f.write(license + '\n')
                f.close()
            print('Successfuly Generated 30 days License: ' + license)
            return(license)


@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$test'):
        await message.channel.send('test!')

    if message.content.startswith('$generate 1d'):
        await message.channel.send('Generating license for 1 day')
        license = license_gen1d()
        await message.channel.send(license)
    
    if message.content.startswith('$generate 7d'):
        await message.channel.send('Generating license for 7 days')
        license = license_gen7d()
        await message.channel.send(license)

    if message.content.startswith('$generate 30d'):
        await message.channel.send('Generating license for 30 days')
        license = license_gen30d()
        await message.channel.send(license)

    if message.content.startswith('$redeem'):
        rcode = re.findall('redeem (.*)', message.content)[0]
        if rcode in l1d:
            await message.channel.send('Successfully Redeemed Code for 1 day use!')
        elif rcode in l7d:
            await message.channel.send('Successfully Redeemed Code for 7 day use!')
        elif rcode in l30d:
            await message.channel.send('Successfully Redeemed Code for 30 day use!')
        else:
            await message.channel.send('Invalid code!')







client.run('token')

向用戶添加和編輯角色,

member = message.author #gets the member to edit
var = discord.utils.get(message.guild.roles, name = "role name")
# This will get the role you want to edit^
member.add_role(var) # adds the role
await var.edit(color=0x008000, reason="The reason")
#and edits the color

有關更多參數,您可以編輯訪問https://discordpy.readthedocs.io/en/latest/api.html?highlight=role#discord.Role如果這有效,請不要忘記將其標記為答案。

如果許可證密鑰正確,您將獲得消息的作者,然后更新他們的角色。 帶有 Member.roles 的Member.roles 如果您搜索它,我認為文檔中有一個示例(帶有代碼)。 我現在無法打開文檔,也不記得確切的命令名稱和參數。

這是我想出的:

member = message.author
role = get(message.guild.roles, name = "test role")
await member.add_roles(role, atomic=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM