Allows user to individually set a prefix to use in prefix commands. It provides two prefix commands: `prefix` explains the current prefix for the server. `!change-prefix <prefix>` changes the prefix for the server. `!` is the default prefix.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const event = context.params.event;
const { channel_id, content } = event;
// Use this function to get the prefix in other files.
// Use like `const prefix = await getUserPrefix();`
const getUserPrefix = async () => {
const prefixMap = await lib.utils.kv['@0.1.16'].get({ key: 'prefix', defaultValue: {} });
return prefixMap[context.params.event.author.id] || '!'
}
// Sets the prefix to the current guild.
const setUserPrefix = async (prefix) => {
const prefixMap = await lib.utils.kv['@0.1.16'].get({ key: 'prefix', defaultValue: {} });
prefixMap[context.params.event.author.id] = prefix;
await lib.utils.kv['@0.1.16'].set({ key: 'prefix', value: prefixMap });
}
// Get prefix command.
if (content === 'prefix') {
const prefix = await getUserPrefix()
return await lib.discord.channels['@0.2.0'].messages.create({
channel_id, content: `Your personal prefix is '${prefix}'`
});
}
// Change prefix command
const commandPrefix = await getUserPrefix();
const changePrefixCommand = `${commandPrefix}change-prefix`
if (content.startsWith(changePrefixCommand)) {
const newPrefix = content.replace(changePrefixCommand, '').trim()
if (newPrefix.length === 0)
return await lib.discord.channels['@0.2.0'].messages.create({
channel_id, content: `Please enter a valid prefix`
});
await setUserPrefix(newPrefix)
return await lib.discord.channels['@0.2.0'].messages.create({
channel_id, content: `Your personal prefix is now '${newPrefix}'`
});
}