简体   繁体   English

通过 Discord.py 获取角色

[英]Getting a role through Discord.py

Good day.再会。 I am new to python programming, I am creating my bot for a server in discord via the discord.pi library.我是 python 编程的新手,我正在通过 discord.pi 库为不和谐的服务器创建我的机器人。 I need the bot to moderate the messages on the server and for the words that are included in the list it assigns a bad role to the offender.我需要机器人来管理服务器上的消息,并且对于列表中包含的单词,它为冒犯者分配了不良角色。 I coped with the moderation of messages, he finds the right words, but there is a problem with the issue of the role.我应对消息的审核,他找到了正确的词,但角色的问题有问题。 I tried many variants of the code for issuing a role, but they all led to an error when the bot tried to issue a role.我尝试了许多用于发布角色的代码变体,但是当机器人尝试发布角色时,它们都导致错误。 Most often this was due to the fact that the creators of this library changed everything in many places.大多数情况下,这是由于该库的创建者在许多地方改变了一切。 How can I solve my problem.我该如何解决我的问题。

I have attached only the role issuance code:我只附上了角色发布代码:

import discord
import asyncio
from discord import utils
import config

authorper = str
messageper = str
member = authorper

class MyClient(discord.Client):
    async def on_ready(self): 
        print('We have logged in as {0}!'.format(self.user))

    async def on_message(self, message):
        member = ctx.guild.get_member(int(ab))
        var = discord.utils.get(message.guild.roles, name = config.ROLE)
        await member.add_role(var)

#RUN
client = MyClient()
client.run(config.TOKEN)

You can add a role by doing await member.add_roles(var)您可以通过执行await member.add_roles(var)添加角色

Make sure you have enabled intents in your code and enabled in discord developer settings as member would be None.确保您在代码中启用了意图并在不和谐的开发人员设置中启用,因为member将为无。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM