This is the code for the bot that we use in the course Discord. It's being built over a series of livestreams and only does a few basic things like replaying livestream chats.
https://learnjsthehardway.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.1 KiB
176 lines
4.1 KiB
import { Client, Intents } from "discord.js";
|
|
import assert from "assert";
|
|
import { TOKEN, chat_secret } from "./secrets/discord.js";
|
|
import { io } from "socket.io-client";
|
|
|
|
const socket = io("ws://localhost:5001");
|
|
|
|
socket.io.on("error", (error) => {
|
|
console.error(error);
|
|
});
|
|
|
|
socket.io.on("ping", () => {
|
|
});
|
|
|
|
socket.on("connect", () => {
|
|
console.log("CONNECTED", socket.connected);
|
|
});
|
|
|
|
|
|
const client = new Client({
|
|
intents: [
|
|
Intents.FLAGS.GUILDS,
|
|
Intents.FLAGS.GUILD_MEMBERS,
|
|
Intents.FLAGS.GUILD_MESSAGES,
|
|
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
|
|
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
|
|
Intents.FLAGS.GUILD_INVITES,
|
|
Intents.FLAGS.DIRECT_MESSAGES
|
|
],
|
|
partials: [ "REACTION", "MESSAGE", "CHANNEL", "GUILD_MEMBER" ],
|
|
allowedMentions: {
|
|
parse: ["users", "roles", "everyone"],
|
|
repliedUser: true
|
|
}
|
|
});
|
|
|
|
const load_guild_channel = async (client) => {
|
|
const guilds = await client.guilds.fetch();
|
|
|
|
let temp = guilds.entries().next().value[1];
|
|
assert(temp, "Can't find any guilds/servers.");
|
|
|
|
const guild = await temp.fetch();
|
|
assert(guild, "No guild after fetch.");
|
|
|
|
const channels = await guild.channels.fetch();
|
|
|
|
for(let chan of channels.entries()) {
|
|
if(chan[1].type === "GUILD_TEXT") {
|
|
// remember, return await puts the errors here
|
|
return [guild, await chan[1].fetch()];
|
|
}
|
|
}
|
|
|
|
// fallthrough, no text channels
|
|
return [guild, undefined];
|
|
}
|
|
|
|
client.login(TOKEN);
|
|
|
|
let [guild, channel] = await load_guild_channel(client);
|
|
assert(channel !== undefined, "Did not find one text channel.");
|
|
|
|
console.log("Connected to Sever/Guild", guild.name);
|
|
console.log("Sending invites from Channel", channel.name);
|
|
|
|
const create_invite = async (channel) => {
|
|
return await channel.createInvite({
|
|
maxAge: 120, maxUses: 1, unique: true
|
|
});
|
|
}
|
|
|
|
client.on("ready", async () => {
|
|
console.log("Ready.");
|
|
});
|
|
|
|
|
|
const all_events = [
|
|
"apiRequest",
|
|
"apiResponse",
|
|
"channelCreate",
|
|
"channelDelete",
|
|
"channelPinsUpdate",
|
|
"channelUpdate",
|
|
"debug",
|
|
"emojiCreate",
|
|
"emojiDelete",
|
|
"emojiUpdate",
|
|
"error",
|
|
"guildBanAdd",
|
|
"guildBanRemove",
|
|
"guildCreate",
|
|
"guildDelete",
|
|
"guildIntegrationsUpdate",
|
|
"guildMemberAdd",
|
|
"guildMemberAvailable",
|
|
"guildMemberRemove",
|
|
"guildMembersChunk",
|
|
"guildMemberUpdate",
|
|
"guildScheduledEventCreate",
|
|
"guildScheduledEventDelete",
|
|
"guildScheduledEventUpdate",
|
|
"guildScheduledEventUserAdd",
|
|
"guildScheduledEventUserRemove",
|
|
"guildUnavailable",
|
|
"guildUpdate",
|
|
"interactionCreate",
|
|
"invalidated",
|
|
"invalidRequestWarning",
|
|
"inviteCreate",
|
|
"inviteDelete",
|
|
"messageDelete",
|
|
"messageDeleteBulk",
|
|
"messageReactionAdd",
|
|
"messageReactionRemove",
|
|
"messageReactionRemoveAll",
|
|
"messageReactionRemoveEmoji",
|
|
"messageUpdate",
|
|
"presenceUpdate",
|
|
"rateLimit",
|
|
"roleCreate",
|
|
"roleDelete",
|
|
"roleUpdate",
|
|
"shardDisconnect",
|
|
"shardError",
|
|
"shardReady",
|
|
"shardReconnecting",
|
|
"shardResume",
|
|
"stageInstanceCreate",
|
|
"stageInstanceDelete",
|
|
"stageInstanceUpdate",
|
|
"stickerCreate",
|
|
"stickerDelete",
|
|
"stickerUpdate",
|
|
"threadCreate",
|
|
"threadDelete",
|
|
"threadListSync",
|
|
"threadMembersUpdate",
|
|
"threadMemberUpdate",
|
|
"threadUpdate",
|
|
"typingStart",
|
|
"userUpdate",
|
|
"voiceStateUpdate",
|
|
"warn",
|
|
"webhookUpdate",
|
|
];
|
|
|
|
|
|
client.on('interactionCreate', interaction => {
|
|
console.log("INTERACTION", interaction);
|
|
});
|
|
|
|
const BAN_LIST = {"zedshaw": true};
|
|
|
|
client.on("messageCreate", async msg => {
|
|
if(msg.channel.type === "DM") {
|
|
console.log("DM FROM", msg.author.username, msg.channel.type, msg.channel.name, msg.content);
|
|
} else if(msg.channel.name === "livestream") {
|
|
console.log(">>> LIVESTREAM CHAT MESSAGE FROM", msg.author.username, msg.channel.type, msg.channel.name, msg.content);
|
|
|
|
const initials = `${msg.author.username[0]}${msg.author.username[1]}`;
|
|
|
|
socket.emit("/chat/message", {
|
|
chat_secret, user_id: initials, text: msg.content
|
|
});
|
|
|
|
} else {
|
|
console.log("IGNORED MESSAGE FROM", msg.author.username, "IN", msg.channel.name);
|
|
}
|
|
});
|
|
|
|
for(let event of all_events) {
|
|
client.on(event, (args) => {
|
|
console.log("EVENT", event, "ARGS", args);
|
|
});
|
|
}
|
|
|