我怎么能得到一个时钟插件工作在我的不和谐机器人与Discordeno库?



所以,我试图为Discord bot编写一个插件,它使用Discordeno作为其库。这个插件理论上应该将语音频道重命名为计算机上的本地时间,这将每分钟进行一次。问题是,它只会在Bot Start上重命名通道,甚至不会重命名通道。

所以,代码通常是这样格式化的:
export async function clock(client: BotWithCache<Bot>) {
const d = new Date()
const conf = config.plugins.clockChannel
function clockEmoji(date: Date) {
const hour = date.toLocaleTimeString('en-US',
{ hour12: true, hour: 'numeric', timeZone: conf.timezone }
).replace(/s(AM|PM)$/, '');
const numToEmoji = {
'12': '🕛',
'0': '🕛',
'1': '🕐',
'2': '🕑',
'3': '🕒',
'4': '🕓',
'5': '🕔',
'6': '🕕',
'7': '🕖',
'8': '🕗',
'9': '🕘',
'10': '🕙',
'11': '🕚'
}
// deno-lint-ignore no-explicit-any
return (numToEmoji as any)[hour] as string
}
const c = dateToString(d, {
clockOnly: true,
includesTimezone: true,
timezone: conf.timezone
})
const chName = conf.channelName!.replace("$TIME", c).replace("$EMOJI", clockEmoji(d))
if (conf.channelID == "0") {
const { id } = await client.helpers.createChannel(config.guildID, {
name: chName,
parentId: conf.categoryID == "0" ? undefined : conf.categoryID,
type: ChannelTypes.GuildVoice,
permissionOverwrites: [{
deny: ["CONNECT"],
id: BigInt(config.guildID),
type: OverwriteTypes.Role
}]
})
config.plugins.clockChannel.channelID = String(id)
const encoder = new TextEncoder();
const data = encoder.encode(JSON.stringify(config, null, 4));
Deno.writeFileSync("config.json", data)
}
editChannel(client, BigInt(conf.channelID!), {
name: chName
})
setInterval(() => {
editChannel(client, BigInt(conf.channelID!), {
name: chName
})
}, 1000 * 60 * (!conf.intervalInMinutes ? 10: conf.intervalInMinutes))
}

这应该做的是拉出bot运行的计算机的时间,并相应地重命名通道。这样会员们就能知道现在是几点了,因为我不是全天候在线的,所以他们可以看看时钟,看看我是不是睡着了。最终发生的是,它将在启动时重命名通道,而不是每分钟重命名它到正确的时间。预期的结果应该是通道被bot重命名为相应的时间。有趣的是,当尝试重命名通道时,控制台输出将每分钟显示相同的时间。我看了看它,我似乎找不到代码

的问题

当前如何设置代码有一个小的逻辑问题。目前,您只需计算一次通道名称,然后继续将通道名称设置为该名称。您应该在每次间隔运行时更新它。

正确的代码应该是这样的:

export async function clock(client: BotWithCache<Bot>) {
const conf = config.plugins.clockChannel
function clockEmoji(date: Date) {
const hour = date.toLocaleTimeString('en-US',
{ hour12: true, hour: 'numeric', timeZone: conf.timezone }
).replace(/s(AM|PM)$/, '');
const numToEmoji = {
'12': '🕛',
'0': '🕛',
'1': '🕐',
'2': '🕑',
'3': '🕒',
'4': '🕓',
'5': '🕔',
'6': '🕕',
'7': '🕖',
'8': '🕗',
'9': '🕘',
'10': '🕙',
'11': '🕚'
}
// deno-lint-ignore no-explicit-any
return (numToEmoji as any)[hour] as string
}
if (conf.channelID == "0") {
const { id } = await client.helpers.createChannel(config.guildID, {
name: chName,
parentId: conf.categoryID == "0" ? undefined : conf.categoryID,
type: ChannelTypes.GuildVoice,
permissionOverwrites: [{
deny: ["CONNECT"],
id: BigInt(config.guildID),
type: OverwriteTypes.Role
}]
})
config.plugins.clockChannel.channelID = String(id);
const data = JSON.stringify(config, null, 4);
Deno.writeTextFileSync("config.json", data);
}
const updateChannel = () => {
const d = new Date();
const c = dateToString(d, {
clockOnly: true,
includesTimezone: true,
timezone: conf.timezone
});
const chName = conf.channelName!.replace("$TIME", c).replace("$EMOJI", clockEmoji(d));

editChannel(client, BigInt(conf.channelID!), {
name: chName
});
}
updateChannel();
const interval = 1000 * 60 * (!conf.intervalInMinutes ? 10: conf.intervalInMinutes);
setInterval(() => {
updateChannel();
}, interval)
}

最新更新