AttributeError: ‘NoneType’ object has no attribute ‘get_role’

huangapple go评论46阅读模式
英文:

AttributeError: 'NoneType' object has no attribute 'get_role'

问题

我试图获取具有特定角色的所有用户但我不知道如何获取这个特定角色我搜索并找到了`guild.get_role(ROLE_ID)`,但不起作用-

import discord
from discord.ext import commands
import os

TOKEN    = # token
GUILD_ID = # guild id
ROLE_ID  = # role id

bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
guild = bot.get_guild(GUILD_ID)

admin_role = guild.get_role(ROLE_ID)

bot.run(TOKEN)

错误
`AttributeError: 'NoneType' object has no attribute 'get_role'`

我不知道如何解决求帮助-
英文:

I'm trying to get all users with a specific role but idk how to get this specific role, I searched and found guild.get_role(ROLE_ID), but doesn't work ;-;

import discord
from discord.ext import commands
import os

TOKEN    = # token
GUILD_ID = # guild id
ROLE_ID  = # role id

bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
guild = bot.get_guild(GUILD_ID)

admin_role = guild.get_role(ROLE_ID)

bot.run(TOKEN)

error:
AttributeError: 'NoneType' object has no attribute 'get_role'

I don't know how to resolve HELP ;-;

答案1

得分: 1

你的变量 guildNone,因为你必须等待你的机器人成功连接到Discord的服务器。为此,你可以简单地监听 on_ready 机器人事件。

import discord
from discord.ext import commands
import os

TOKEN    = # token
GUILD_ID = # guild id
ROLE_ID  = # role id

bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())

@bot.event
async def on_ready():
  guild = bot.get_guild(GUILD_ID)
  admin_role = guild.get_role(ROLE_ID)
  # [...]

bot.run(TOKEN)

还请确保你的公会ID是正确的且是整数。否则 bot.get_guild() 仍然会返回 None

英文:

Your variable guild is None because you have to wait for your bot to successfully connect to Discord's servers first. For this, you can simply listen for the on_ready bot event.

import discord
from discord.ext import commands
import os

TOKEN    = # token
GUILD_ID = # guild id
ROLE_ID  = # role id

bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())

@bot.event
async def on_ready():
  guild = bot.get_guild(GUILD_ID)   
  admin_role = guild.get_role(ROLE_ID)
  # [...] 

bot.run(TOKEN)

Also make sure that your guild ID is correct and an Integer. Otherwise bot.get_guild() will still return None.

huangapple
  • 本文由 发表于 2023年2月24日 06:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定