mirror of
https://github.com/keanuplayz/TravBot-v3.git
synced 2024-08-15 02:33:12 +00:00
Ported events and welcome event
This commit is contained in:
parent
6a4107c01e
commit
ee9c88996e
8 changed files with 1074 additions and 48 deletions
944
package-lock.json
generated
944
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -5,6 +5,7 @@
|
|||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"canvas": "^2.7.0",
|
||||
"chalk": "^4.1.0",
|
||||
"discord.js": "^12.5.1",
|
||||
"discord.js-lavalink-lib": "^0.1.8",
|
||||
|
|
|
@ -55,6 +55,79 @@ export default new Command({
|
|||
$.channel.send(`The custom prefix for this guild is now \`${$.args[0]}\`.`);
|
||||
}
|
||||
})
|
||||
}),
|
||||
welcome: new Command({
|
||||
description: "Configure your server's welcome settings for the bot.",
|
||||
usage: "type/channel <...>",
|
||||
run: "You need to specify which part to modify, `type`/`channel`.",
|
||||
subcommands: {
|
||||
type: new Command({
|
||||
description:
|
||||
"Sets how welcome messages are displayed for your server. Removes welcome messages if unspecified.",
|
||||
usage: "`none`/`text`/`graphical`",
|
||||
async run($) {
|
||||
if ($.guild) {
|
||||
Storage.getGuild($.guild.id).welcomeType = "none";
|
||||
Storage.save();
|
||||
$.channel.send("Set this server's welcome type to `none`.");
|
||||
} else {
|
||||
$.channel.send("You must use this command in a server.");
|
||||
}
|
||||
},
|
||||
// I should probably make this a bit more dynamic... Oh well.
|
||||
subcommands: {
|
||||
text: new Command({
|
||||
async run($) {
|
||||
if ($.guild) {
|
||||
Storage.getGuild($.guild.id).welcomeType = "text";
|
||||
Storage.save();
|
||||
$.channel.send("Set this server's welcome type to `text`.");
|
||||
} else {
|
||||
$.channel.send("You must use this command in a server.");
|
||||
}
|
||||
}
|
||||
}),
|
||||
graphical: new Command({
|
||||
async run($) {
|
||||
if ($.guild) {
|
||||
Storage.getGuild($.guild.id).welcomeType = "graphical";
|
||||
Storage.save();
|
||||
$.channel.send("Set this server's welcome type to `graphical`.");
|
||||
} else {
|
||||
$.channel.send("You must use this command in a server.");
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}),
|
||||
channel: new Command({
|
||||
description: "Sets the welcome channel for your server. Type `#` to reference the channel.",
|
||||
usage: "<channel mention>",
|
||||
run: "You need to specify a channel.",
|
||||
// If/when channel types come out, this will be the perfect candidate to test it.
|
||||
any: new Command({
|
||||
async run($) {
|
||||
if ($.guild) {
|
||||
const match = $.args[0].match(/^<#(\d{17,19})>$/);
|
||||
|
||||
if (match) {
|
||||
Storage.getGuild($.guild.id).welcomeChannel = match[1];
|
||||
Storage.save();
|
||||
$.channel.send(
|
||||
`Successfully set this server's welcome channel to ${match[0]}.`
|
||||
);
|
||||
} else {
|
||||
$.channel.send(
|
||||
"You must provide a reference channel. You can do this by typing `#` then searching for the proper channel."
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$.channel.send("You must use this command in a server.");
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}),
|
||||
|
|
|
@ -51,9 +51,24 @@ class User {
|
|||
|
||||
class Guild {
|
||||
public prefix: string | null;
|
||||
public welcomeType: "none" | "text" | "graphical";
|
||||
public welcomeChannel: string | null;
|
||||
|
||||
constructor(data?: GenericJSON) {
|
||||
this.prefix = select(data?.prefix, null, String);
|
||||
this.welcomeChannel = select(data?.welcomeChannel, null, String);
|
||||
|
||||
switch (data?.welcomeType) {
|
||||
case "text":
|
||||
this.welcomeType = "text";
|
||||
break;
|
||||
case "graphical":
|
||||
this.welcomeType = "graphical";
|
||||
break;
|
||||
default:
|
||||
this.welcomeType = "none";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,8 +3,12 @@ import $ from "../core/lib";
|
|||
import {updateGlobalEmoteRegistry} from "../core/lib";
|
||||
|
||||
export default new Event<"guildCreate">({
|
||||
on() {
|
||||
$.log("Updated emote registry.");
|
||||
on(guild) {
|
||||
$.log(
|
||||
`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot. Owner: ${guild.owner!.user.tag} (${
|
||||
guild.owner!.user.id
|
||||
}). Updated emote registry.`
|
||||
);
|
||||
updateGlobalEmoteRegistry();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -3,8 +3,8 @@ import $ from "../core/lib";
|
|||
import {updateGlobalEmoteRegistry} from "../core/lib";
|
||||
|
||||
export default new Event<"guildDelete">({
|
||||
on() {
|
||||
$.log("Updated emote registry.");
|
||||
on(guild) {
|
||||
$.log(`[GUILD LEAVE] ${guild.name} (${guild.id}) removed the bot. Updated emote registry.`);
|
||||
updateGlobalEmoteRegistry();
|
||||
}
|
||||
});
|
||||
|
|
73
src/events/guildMemberAdd.ts
Normal file
73
src/events/guildMemberAdd.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import Event from "../core/event";
|
||||
import $, {parseVars} from "../core/lib";
|
||||
import {createCanvas, loadImage, Canvas} from "canvas";
|
||||
import {Storage} from "../core/structures";
|
||||
import {TextChannel, MessageAttachment} from "discord.js";
|
||||
|
||||
function applyText(canvas: Canvas, text: string) {
|
||||
const ctx = canvas.getContext("2d");
|
||||
let fontSize = 70;
|
||||
|
||||
do {
|
||||
ctx.font = `${(fontSize -= 10)}px sans-serif`;
|
||||
} while (ctx.measureText(text).width > canvas.width - 300);
|
||||
|
||||
return ctx.font;
|
||||
}
|
||||
|
||||
export default new Event<"guildMemberAdd">({
|
||||
async on(member) {
|
||||
const {welcomeType, welcomeChannel} = Storage.getGuild(member.guild.id);
|
||||
|
||||
if (welcomeChannel) {
|
||||
const channel = member.guild.channels.cache.get(welcomeChannel);
|
||||
|
||||
if (channel && channel.type === "text") {
|
||||
if (welcomeType === "graphical") {
|
||||
const canvas = createCanvas(700, 250);
|
||||
const ctx = canvas.getContext("2d");
|
||||
const background = await loadImage(
|
||||
"https://raw.githubusercontent.com/keanuplayz/TravBot/dev/assets/wallpaper.png"
|
||||
);
|
||||
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
ctx.strokeStyle = "#74037b";
|
||||
ctx.strokeRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
ctx.font = "28px sans-serif";
|
||||
ctx.fillStyle = "#ffffff";
|
||||
ctx.fillText("Welcome to the server,", canvas.width / 2.5, canvas.height / 3.5);
|
||||
|
||||
ctx.font = applyText(canvas, member.displayName);
|
||||
ctx.fillStyle = "#ffffff";
|
||||
ctx.fillText(`${member.displayName}!`, canvas.width / 2.5, canvas.height / 1.5);
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
|
||||
ctx.closePath();
|
||||
ctx.clip();
|
||||
|
||||
const avatarURL =
|
||||
member.user.avatarURL({
|
||||
dynamic: true,
|
||||
size: 2048,
|
||||
format: "png"
|
||||
}) ?? member.user.defaultAvatarURL;
|
||||
const avatar = await loadImage(avatarURL);
|
||||
ctx.drawImage(avatar, 25, 25, 200, 200);
|
||||
|
||||
const attachment = new MessageAttachment(canvas.toBuffer("image/png"), "welcome-image.png");
|
||||
(channel as TextChannel).send(`Welcome \`${member.user.tag}\`!`, attachment);
|
||||
} else if (welcomeType === "text") {
|
||||
(channel as TextChannel).send(
|
||||
parseVars("Say hello to `%user%`, everyone! We all need a warm welcome sometimes :D", {
|
||||
user: member.user.tag
|
||||
})
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$.error(`"${welcomeChannel}" is not a valid text channel ID!`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
|
@ -7,7 +7,9 @@ import {updateGlobalEmoteRegistry} from "../core/lib";
|
|||
export default new Event<"ready">({
|
||||
once() {
|
||||
if (client.user) {
|
||||
$.ready(`Logged in as ${client.user.username}#${client.user.discriminator}.`);
|
||||
$.ready(
|
||||
`Logged in as ${client.user.tag}, ready to serve ${client.users.cache.size} users in ${client.guilds.cache.size} servers..`
|
||||
);
|
||||
client.user.setActivity({
|
||||
type: "LISTENING",
|
||||
name: `${Config.prefix}help`
|
||||
|
|
Loading…
Reference in a new issue