为什么 Guild.get_channel 有时会在 discord.py 中返回一个 none 对象
Why is Guild.get_channel returning a none object sometimes in discord.py
所以我得到了这段代码,但是 get_channel 通常 returns 一个 none 对象,但有时它会回复不和谐频道对象。为什么会这样?有没有更好的办法?
该脚本用于在频道中显示服务器的统计信息。
#Checking the channels & Server Stats
@bot.event
async def on_connect():
while True:
#Checking the channels
filename = "/home/pi/discordbot/CCreated.json"
with open('/home/pi/discordbot/CCreated.json', 'r') as f:
channels_created = json.load(f)
for content in channels_created['timers']:
# find raiding
Guild = bot.get_guild(771495836701425725)
channel_id = int(content)
# finds the members ([] when noone is in)
voice_channel = bot.get_channel(channel_id)
voice_ch_str = str(voice_channel.members)
#Testing if someone is in there
if voice_ch_str == "[]":
channel_get = bot.get_channel(channel_id)
await channel_get.delete()
with open("/home/pi/discordbot/CCreated.json", "r") as f:
data = json.load(f)
data["timers"].pop(f"{data['timers'][content]['channel_id']}")
with open("/home/pi/discordbot/CCreated.json", "w") as f:
json.dump(data, f, indent=2)
#Server Stats
#Getting User Count
Guild = bot.get_guild(771495836701425725)
member_count = len(Guild.members) # includes bots
true_member_count = len([m for m in Guild.members if not m.bot]) # doesn't include bots
#Getting Streamer Count
role = Guild.get_role(841596562689097729)
#print(type(role))
#print(len(role.members))
streamer_count = len(role.members)
#Setting the new Stats
streamer_stats = Guild.get_channel(968570363103568012)
await streamer_stats.edit(name=f"\U0001F4CA STREAMER: {streamer_count}")
get
和 Client
class 中定义的 fetch
方法之间存在一个关键区别。区别在于:get
returns cached 机器人结果,而 fetch
returns API 调用,又名请求,提供有关所请求项目的全局信息。
这有一些缺点。
使用 fetch
通常 returns objects/instances 使用的属性少得多,因为全局信息不如缓存信息那么多,缓存信息本质上是机器人本身可以看到的项目。简而言之:
Client.get_channel(some_id) # returns a cached channel, has all the documented attributes
# not the same as below
await Client.fetch_channel(some_id) # returns a channel fetched directly from the API, does lack a few of the documented attributes
我修好了。
我将角色检查放在成员更新中并针对特定角色进行“过滤”,每当有人获得或丢失它时,它都会检查该角色拥有多少用户,然后设置频道名称。
用户计数现在在 on_member_remove /join 中,每次发生这种情况时,机器人都会检查服务器上有多少用户并设置频道名称。
所以我得到了这段代码,但是 get_channel 通常 returns 一个 none 对象,但有时它会回复不和谐频道对象。为什么会这样?有没有更好的办法? 该脚本用于在频道中显示服务器的统计信息。
#Checking the channels & Server Stats
@bot.event
async def on_connect():
while True:
#Checking the channels
filename = "/home/pi/discordbot/CCreated.json"
with open('/home/pi/discordbot/CCreated.json', 'r') as f:
channels_created = json.load(f)
for content in channels_created['timers']:
# find raiding
Guild = bot.get_guild(771495836701425725)
channel_id = int(content)
# finds the members ([] when noone is in)
voice_channel = bot.get_channel(channel_id)
voice_ch_str = str(voice_channel.members)
#Testing if someone is in there
if voice_ch_str == "[]":
channel_get = bot.get_channel(channel_id)
await channel_get.delete()
with open("/home/pi/discordbot/CCreated.json", "r") as f:
data = json.load(f)
data["timers"].pop(f"{data['timers'][content]['channel_id']}")
with open("/home/pi/discordbot/CCreated.json", "w") as f:
json.dump(data, f, indent=2)
#Server Stats
#Getting User Count
Guild = bot.get_guild(771495836701425725)
member_count = len(Guild.members) # includes bots
true_member_count = len([m for m in Guild.members if not m.bot]) # doesn't include bots
#Getting Streamer Count
role = Guild.get_role(841596562689097729)
#print(type(role))
#print(len(role.members))
streamer_count = len(role.members)
#Setting the new Stats
streamer_stats = Guild.get_channel(968570363103568012)
await streamer_stats.edit(name=f"\U0001F4CA STREAMER: {streamer_count}")
get
和 Client
class 中定义的 fetch
方法之间存在一个关键区别。区别在于:get
returns cached 机器人结果,而 fetch
returns API 调用,又名请求,提供有关所请求项目的全局信息。
这有一些缺点。
使用 fetch
通常 returns objects/instances 使用的属性少得多,因为全局信息不如缓存信息那么多,缓存信息本质上是机器人本身可以看到的项目。简而言之:
Client.get_channel(some_id) # returns a cached channel, has all the documented attributes
# not the same as below
await Client.fetch_channel(some_id) # returns a channel fetched directly from the API, does lack a few of the documented attributes
我修好了。 我将角色检查放在成员更新中并针对特定角色进行“过滤”,每当有人获得或丢失它时,它都会检查该角色拥有多少用户,然后设置频道名称。
用户计数现在在 on_member_remove /join 中,每次发生这种情况时,机器人都会检查服务器上有多少用户并设置频道名称。