【问题标题】:I can't get certain guild with discord.py我无法通过 discord.py 获得某些公会
【发布时间】:2021-01-02 12:50:20
【问题描述】:

我正在尝试在机器人启动时扮演某些角色。首先我需要加入公会,但我做不到。

takenGuild = client.get_guild(myServerID)

takenGuild 返回无

当我尝试循环公会时

for guild in client.guilds:
    print(guild.id)

它没有打印任何东西。

我的整个代码:

import discord
from discord.ext import commands, tasks
from discord.utils import get

client = commands.Bot(command_prefix = '.')


takenGuild = client.get_guild(123123123123123123)
print(takenGuild.id)

for guild in client.guilds:
    print(guild)
    print(guild.id)
    
    
client.run('Token')

【问题讨论】:

  • 你的机器人必须是公会的一部分才能访问它,这意味着你必须先邀请你的机器人。
  • 我已经邀请了机器人。它在服务器上,但当然离线。
  • 请发布您的整个代码,因为目前尚不清楚您要在哪里循环遍历您的行会。

标签: python discord.py


【解决方案1】:

您必须等待机器人准备好才能使用它。仅供参考,更新后botclient 更常见

import discord
from discord.ext import commands, tasks
from discord.utils import get

bot = commands.Bot(command_prefix = '.')

@bot.event
async def on_ready():
    print("I am running on " + bot.user.name)
    print("With the ID: " + bot.user.id)
    print('Bot is ready to be used')
   # after it is ready do it

    takenGuild = bot.get_guild(123123123123123123)
    print(takenGuild.id)

    for guild in bot.guilds:
        print(guild)
        print(guild.id)
    
    
bot.run('Token')

【讨论】:

  • 昨天我尝试这个时它没有工作,也许我输入了错误的服务器ID。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-12-03
  • 2020-09-25
  • 2016-06-08
  • 2017-01-25
  • 2015-10-26
  • 2019-07-27
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多