简体   繁体   English

Discord.py 将角色添加到成员

[英]Discord.py Add Role to member

I'm currently working on discord.py bot that'll be used to manage licenses on discord server.我目前正在研究 discord.py 机器人,它将用于管理 discord 服务器上的许可证。 Currently stuck on how to add role after successful redeem of code.目前卡在成功兑换代码后如何添加角色。 I added 3 roles in my discord server(1Day, 7Days, 30Days), how do I make bot to add role to user who successfully redeemed the license?我在我的 discord 服务器中添加了 3 个角色(1 天、7 天、30 天),如何让机器人为成功兑换许可证的用户添加角色?

Here's my code for now:这是我现在的代码:

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')

Adding and editing roles to a user,向用户添加和编辑角色,

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

For more parameters you can edit visit https://discordpy.readthedocs.io/en/latest/api.html?highlight=role#discord.Role If this works don't forget to mark it as answer.有关更多参数,您可以编辑访问https://discordpy.readthedocs.io/en/latest/api.html?highlight=role#discord.Role如果这有效,请不要忘记将其标记为答案。

If the license key is correct you get the author of the message, then update their roles.如果许可证密钥正确,您将获得消息的作者,然后更新他们的角色。 with a function of Member.roles .带有 Member.roles 的Member.roles I think the documentation has an example of it (with code) if you search it up.如果您搜索它,我认为文档中有一个示例(带有代码)。 I am not able to open the docs right now and I don't remember the exact command name and parameters.我现在无法打开文档,也不记得确切的命令名称和参数。

Here's what I came up with:这是我想出的:

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