简体   繁体   English

赋予机器人角色的问题

[英]Issues giving role to the bot

I am creating a discord bot to change its own role.我正在创建一个 discord 机器人来改变自己的角色。 Currently I am just trying to give it a role.目前我只是想给它一个角色。

import json
import discord
import os
import bs4
from dotenv import load_dotenv
from discord.ext import commands
from datetime import datetime
import time
import re
from string import digits, ascii_letters
from discord.ext.commands import Bot
import aiohttp
from discord.utils import get

load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
bot = commands.Bot(command_prefix="$")
bot.remove_command('help')
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")

url = "hidden"

@bot.event
async def on_ready():
    await bot.wait_until_ready()
    bot.session = aiohttp.ClientSession()
    print("It's ready - fetching btc price")
    while True:
        async with bot.session.get(url) as resp:
            resp = await resp.text()
            auth = [MY BOTS ID]
            role = [ROLE ID, I.E 9234824234626534]
            await bot.add_roles(auth, role)

error AttributeError: 'Bot' object has no attribute 'add_roles'错误AttributeError: 'Bot' object has no attribute 'add_roles'

How can I give my bot a role or remove a role?我怎样才能给我的机器人一个角色或删除一个角色? It must be in def on_ready()它必须在 def on_ready()

To add roles, you need the member object.要添加角色,您需要member object。 To get this, you need to know the guild that the member is in, so this would be impossible in on_ready() unless you wanted to iterate through every single guild in the guild cache ( for guild in bot.guilds: if you're really interested in doing that, but still be sure to read the rest of this).为了得到这个,你需要知道成员所在的公会,所以这在on_ready()中是不可能的,除非你想遍历公会缓存中的每个公会( for guild in bot.guilds:如果你是真的有兴趣这样做,但仍然一定要阅读 rest 这个)。

First off, to even be able to obtain a member object, you need to have your intents enabled.首先,要获得member object,您需要启用您的意图 You can do this by just changing your bot ClientUser to commands.Bot(command_prefix="$", intents = discord.Intents.all()) , as well as enabling your intents enabled on the Developer Portal .您可以通过将您的bot ClientUser 更改为commands.Bot(command_prefix="$", intents = discord.Intents.all())以及在Developer Portal上启用您的意图来做到这一点。

For the example that I'll show you I will be using a command, but if you really want to use the on_ready() function you can by just replacing every ctx.guild with guild as you iterate through the guild cache as seen above.对于我将向您展示的示例,我将使用命令,但如果您真的想使用on_ready() function,您可以在迭代公会缓存时将每个ctx.guild替换为guild ,如上所示。 I also used the discord.utils.get method to define my role, any method that you use is fine so long as you end up with a role object我还使用了discord.utils.get方法来定义我的角色,您使用的任何方法都可以,只要您最终获得角色object

@bot.command()
def addRole(ctx):
    role = discord.utils.get(ctx.guild.roles, name = "Role Name")
    member = ctx.guild.get_member(bot.user.id)
    await member.add_roles(role)

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

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