通道缓存不连贯

Posted

技术标签:

【中文标题】通道缓存不连贯【英文标题】:Channel cache is incoherent 【发布时间】:2021-10-07 09:46:15 【问题描述】:

我已经构建了一些函数来表频道和类别信息,其中一个在机器人开始时运行以确保所有内容都同步。

我遇到的问题是bot.channels.cache 包含不再存在的通道,或者不再存在的通道状态。例如,我在服务器中只有一个名为“general”的频道。然而,该频道的名称有 3 个单独的条目,其中只有一个包含当前“常规”频道的 ID (711043006781849686):

import Discord from 'discord.js'
import config from '../config.js'

const bot = new Discord.Client( partials: ['MESSAGE', 'CHANNEL', 'REACTION'] )

bot.login(config.botToken)

bot.on('ready', async () => 
  console.log(bot.channels.cache)


-- returns --

Collection(46) [Map] 
  ...
  '711043006781849686' => <ref *16> TextChannel 
    type: 'text',
    deleted: false,
    id: '711043006781849686',
    name: 'general',
    rawPosition: 10,
    parentID: '711043007197216880',
    permissionOverwrites: Collection(3) [Map] 
      '711043006253367426' => [PermissionOverwrites],
      '711043006295179347' => [PermissionOverwrites],
      '861109585930747934' => [PermissionOverwrites]
    ,
    topic: 'General chat channel.',
    nsfw: false,
    lastMessageID: '860794574707752980',
    rateLimitPerUser: 0,
    lastPinTimestamp: null,
    guild: Guild 
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '711043006253367426',
      shardID: 0,
      name: 'Omegabox',
      icon: null,
      splash: null,
      discoverySplash: null,
      region: 'us-central',
      memberCount: 5,
      large: false,
      features: [Array],
      applicationID: null,
      afkTimeout: 900,
      afkChannelID: '711043009944223832',
      systemChannelID: '711043006781849686',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'NONE',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1589597389528,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      maximumMembers: 100000,
      maximumPresences: null,
      approximateMemberCount: null,
      approximatePresenceCount: null,
      vanityURLCode: null,
      vanityURLUses: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      preferredLocale: 'en-US',
      ownerID: '598729034867933195',
      emojis: [GuildEmojiManager]
    ,
    messages: MessageManager 
      cacheType: [class LimitedCollection extends Collection],
      cache: [LimitedCollection [Map]],
      channel: [Circular *16]
    ,
    _typing: Map(0) 
  ,
  ...
  '827343616678559757' => <ref *33> TextChannel 
    type: 'text',
    deleted: false,
    id: '827343616678559757',
    name: 'general',
    rawPosition: 0,
    parentID: '827343616678559755',
    permissionOverwrites: Collection(0) [Map] ,
    topic: null,
    lastMessageID: '830245759152291860',
    rateLimitPerUser: 0,
    lastPinTimestamp: null,
    guild: Guild 
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '827343616678559754',
      shardID: 0,
      name: 'Megabox Emojis 1',
      icon: null,
      splash: null,
      discoverySplash: null,
      region: 'us-west',
      memberCount: 3,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '827343616678559757',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'NONE',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1617380998194,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      maximumMembers: 100000,
      maximumPresences: null,
      approximateMemberCount: null,
      approximatePresenceCount: null,
      vanityURLCode: null,
      vanityURLUses: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      preferredLocale: 'en-US',
      ownerID: '598729034867933195',
      emojis: [GuildEmojiManager]
    ,
    messages: MessageManager 
      cacheType: [class LimitedCollection extends Collection],
      cache: [LimitedCollection [Map]],
      channel: [Circular *33]
    ,
    nsfw: false,
    _typing: Map(0) 
  ,
  ...
  '827344454259703842' => <ref *34> TextChannel 
    type: 'text',
    deleted: false,
    id: '827344454259703842',
    name: 'general',
    rawPosition: 0,
    parentID: '827344454259703840',
    permissionOverwrites: Collection(0) [Map] ,
    topic: null,
    lastMessageID: '827580681730261032',
    rateLimitPerUser: 0,
    lastPinTimestamp: null,
    guild: Guild 
      members: [GuildMemberManager],
      channels: [GuildChannelManager],
      roles: [RoleManager],
      presences: [PresenceManager],
      voiceStates: [VoiceStateManager],
      deleted: false,
      available: true,
      id: '827344454259703838',
      shardID: 0,
      name: 'Megabox Emojis 2',
      icon: null,
      splash: null,
      discoverySplash: null,
      region: 'us-west',
      memberCount: 3,
      large: false,
      features: [],
      applicationID: null,
      afkTimeout: 300,
      afkChannelID: null,
      systemChannelID: '827344454259703842',
      embedEnabled: undefined,
      premiumTier: 0,
      premiumSubscriptionCount: 0,
      verificationLevel: 'NONE',
      explicitContentFilter: 'DISABLED',
      mfaLevel: 0,
      joinedTimestamp: 1617381010142,
      defaultMessageNotifications: 'ALL',
      systemChannelFlags: [SystemChannelFlags],
      maximumMembers: 100000,
      maximumPresences: null,
      approximateMemberCount: null,
      approximatePresenceCount: null,
      vanityURLCode: null,
      vanityURLUses: null,
      description: null,
      banner: null,
      rulesChannelID: null,
      publicUpdatesChannelID: null,
      preferredLocale: 'en-US',
      ownerID: '598729034867933195',
      emojis: [GuildEmojiManager]
    ,
    messages: MessageManager 
      cacheType: [class LimitedCollection extends Collection],
      cache: [LimitedCollection [Map]],
      channel: [Circular *34]
    ,
    nsfw: false,
    _typing: Map(0) 
  

我知道有一个名为“General”的类别(ID:711043007197216880),但在条目中维护的大小写不同。我已经过滤掉了那个,以及上面块中不是“一般”的所有其他内容。

我在这里缺少什么?可以同步吗?

【问题讨论】:

【参考方案1】:

总能在终于发帖后顿悟。

在这工作了好几个小时,但从未意识到在这个特定的电话中我没有指定什么公会,或者更确切地说是什么服务器。该机器人位于多个服务器中,并且我已经在我正在使用的配置中获得了当前服务器的 ID。

bot.channels.cache 更改为bot.guilds.cache.get(config.guildId).channels.cache

【讨论】:

以上是关于通道缓存不连贯的主要内容,如果未能解决你的问题,请参考以下文章

面向对象、面向数据、缓存污染和缓存明显性

更好地理解 PTX 存储缓存模式

redis基础系列~缓存应用场景

WCF ChannelFactory 和通道 - 缓存、重用、关闭和恢复

Java并发基础

盘一盘 NIO —— Channel解析