So, I tried to write a plugin for a Discord bot, which uses Discordeno for its Library. This plugin was supposed to theoretically rename a Voice channel to the Local Time on the computer, and this would be done every minute. Issue is, it will only rename the channel on the Bot Start, and will not even rename the channel.
So, this is how the code was usually formatted:
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))
}
What this is supposed to do, is pull the time of the computer the bot runs on, and rename the channel accordingly. This would allow members to know what time it is, cause I'm not online 24/7, so they can look at the clock and see if maybe I'm asleep or something. What ends up happening is it will rename the channel on start, and not rename it to the correct time every minute. The expected Result should have been that the channel gets renamed by the bot to the corresponding time. The interesting thing is that the console output, when trying to rename the channel, will show the same time every minute. I looked at it, and I cannot seem to find the issue with the code
There's a minor logical issue with how your code is currently set up. Currently, you calculate the name of the channel once and then keep setting the channel name to that one thing. You should instead update it every time the interval runs.
The corrected code should look like:
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)
}