Skip to content

Discord Bot

This example uses Discord.JS with TypeScript

Package Install:

$
npm install discord.js @fleco/duration
import { Client, Events, Interaction, GatewayIntentBits } from "discord.js";
import { Duration } from "@fleco/duration";
import { token } from "./config.json";

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildMembers
]
});

client.once(Events.ClientReady, (client: Client) => {
console.log(`Logged in as ${client.user.tag}`)
/_ ... Command Registration Code ... _/
});

client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === "mute") {
    	const memberFetch = interaction.options.getUser('user', true);
    	const member = await interaction.guild?.members.fetch(memberFetch!.id);
    	const durStr = interaction.options.getString('duration', true);

    	const duration = new Duration(durStr);

    	await member.timeout(duration.duration.abs().milliseconds, '{INSERT DISCORD MOD COMMENT HERE}');

    	await interaction.reply(`Muted <@${member.id}>`);
    }

});

client.login(token);