Skip to content

Commit

Permalink
Updated Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pakkographic committed Jul 3, 2024
1 parent 2eb5b70 commit 24e34af
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 58 deletions.
12 changes: 6 additions & 6 deletions examples/badword.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Client, Member } = require("touchguild");

// Creating client & connecting.
const client = new Client({
token: "TOKEN"
token: "INSERT TOKEN"
});

client.on("ready", () => {
Expand All @@ -15,16 +15,16 @@ client.on("error", (err) => {
});

const badWords = [
"fuck",
"bitch",
"dick"
]
"take the L",
"noob",
"looser"
];

client.on("messageCreate", async (message) => {
const member = await message.member;
if (member.bot) return;
if (member instanceof Member) {
if (badWords.some(badword => message.content.includes(badword))) {
if (badWords.some(badWord => message.content.includes(badWord))) {
message.delete().then(() => console.log("Successfully deleted the swear."))
.catch(err => console.log("Failed to delete the swear."));
member.ban(`bad word: ${message.content}`)
Expand Down
2 changes: 1 addition & 1 deletion examples/ping.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Client } = require("touchguild");
const client = new Client({ token: "TOKEN" });
const client = new Client({ token: "INSERT TOKEN" });

client.on("ready", () => {
console.log("Ready as", client.user.username);
Expand Down
110 changes: 77 additions & 33 deletions examples/simpleBot.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
const { Client } = require("touchguild");
// Imports
const { Client } = require("touchguild"); // JavaScript/TS (CommonJS)
// TypeScript/JavaScript (ES): import { Client } from "touchguild";

// Configuration
const config = {
token: "TOKEN",
token: "INSERT TOKEN",
prefix: "/"
}

// Constructing the Client, passing the token into it.
const client = new Client({ token: config.token });

// Map that will contain settings for each registered guild.
const guildSettingsMap = new Map();

// Listening to the Client 'ready' state that is emitted.
client.on("ready", () => {
console.log("Ready as", client.user?.username, "(" + client.user?.id + ")");
});

// Listening to new messages being sent.
client.on("messageCreate", async (message) => {
if ((await message.member)?.bot === true) return;
// Check if the user is a bot or not, if yes, return
// preventing potential message creation loop when making out our commands
if ((await message.member)?.bot) return;
// Check if the message content starts with the set prefix
if (message.content?.startsWith(config.prefix)) {
const msgcontent = message.content.toLowerCase().split(config.prefix)[1];
switch (msgcontent) {
// Split the message at the prefix symbol
// so we can separate the prefix from the message content
const msgContent = message.content.toLowerCase().split(config.prefix)[1];
switch (msgContent) {
// First command, 'help', a list of command you can use
case "help": {
const helpEmbed = {
title: "Help",
description: "Every commands you can use.",
description: "Here are all the commands you can use:",
fields: [
{ name: `${config.prefix}help`, value: "shows every command you can use." },
{ name: `${config.prefix}uptime`, value: "bot up time." },
{ name: `${config.prefix}latency`, value: "bot's latency in ms." },
{ name: `${config.prefix}hi`, value: "say hi" },
{ name: `${config.prefix}ping`, value: "pong" },
{ name: `${config.prefix}pong`, value: "ping" },
{ name: `${config.prefix}cmdnotfound`, value: "Disable the command not found message." }
{ name: `${config.prefix }help`, value: "shows all the command you can use." },
{ name: `${config.prefix }uptime`, value: "bot up time." },
{ name: `${config.prefix }latency`, value: "bot's latency in ms." },
{ name: `${config.prefix }hi`, value: "say hi" },
{ name: `${config.prefix }ping`, value: "pong" },
{ name: `${config.prefix }pong`, value: "ping" },
{ name: `${config.prefix }not_found`, value: "Disable the command not found message." }
]
}
return message.createMessage({ embeds: [helpEmbed], replyMessageIds: [message.id] });
return message.createMessage({
embeds: [helpEmbed],
replyMessageIds: [message.id]
});
}
// Uptime command, showing precisely for how long
// the application is up.
case "uptime": {
const uptime = new Date(client.uptime);
const days = uptime.getDate() - 1;
Expand All @@ -41,55 +63,77 @@ client.on("messageCreate", async (message) => {
{
title: "Client uptime",
fields: [
{ name: "Days", value: days.toString(), inline: true },
{ name: "Hours", value: hours.toString(), inline: true },
{ name: "Minutes", value: mins.toString(), inline: true },
{ name: "Seconds", value: secs.toString(), inline: true }
{ name: "Days", value: days.toString(), inline: true },
{ name: "Hours", value: hours.toString(), inline: true },
{ name: "Minutes", value: mins.toString(), inline: true },
{ name: "Seconds", value: secs.toString(), inline: true }
]
}
]
});
}
// Latency command, gives the bot latency in ms
case "latency": {
const latency = Date.now() - message.createdAt.getTime();
return message.createMessage({ embeds: [{ title: "Latency: " + latency + "ms" }] })
}
// Simple hi command
case "hi": {
return message.createMessage({ content: "hi", replyMessageIds: [message.id] });
}
// ping!
case "ping": {
return message.createMessage({ content: "pong!", replyMessageIds: [message.id] })
}
// pong!
case "pong": {
return message.createMessage({ content: "ping!", replyMessageIds: [message.id] });
}
case "cmdnotfound": {
if (!guildSettingsMap.has(message.guildID) && message.guildID) setGuildSettings(message.guildID); else if (!message.guildID) return message.createMessage({ content: "Something went wrong." });
// Toggle the command not found message if it is annoying,
// or if you're using another bot with the same prefix (in both case it is annoying)
case "not_found": {
if (!guildSettingsMap.has(message.guildID) && message.guildID)
setGuildSettings(message.guildID);
else if (!message.guildID)
return message.createMessage({ content: "Something went wrong." });

// get settings object
const settings = guildSettingsMap.get(message.guildID);
if (settings["cmdnotfound"] == true) {
settings["cmdnotfound"] = false;
} else if (settings["cmdnotfound"] == false) {
settings["cmdnotfound"] = true;
}
// toggle not_found boolean
settings["not_found"] = !settings["not_found"];
// replace the old settings object with the new one
guildSettingsMap.set(message.guildID, settings);
message.createMessage({ content: `Successfully ${settings["cmdnotfound"] ? "enabled" : "disabled"} cmdnotfound.` })
// send success message
void message.createMessage({
content: `Successfully ${settings["not_found"] ? "enabled" : "disabled"} not_found.`
});
break;
}
// The command not found message,
// if the command the user is trying to execute is unknown
default: {
if (guildSettingsMap.get(message.guildID) == undefined && message.guildID) setGuildSettings(message.guildID);
if (guildSettingsMap.get(message.guildID)?.["cmdnotfound"] == true) {
message.createMessage({ content: "command not found", replyMessageIds: [message.id] });
}
if (guildSettingsMap.get(message.guildID) == undefined && message.guildID)
setGuildSettings(message.guildID);
if (guildSettingsMap.get(message.guildID)?.["not_found"])
void message.createMessage({
content: "command not found", replyMessageIds: [message.id]
});
}
}
} else if (message.mentions && message.mentions.users?.find(user => user.id == client.user?.id)) {
message.createMessage({ content: `haha, stop pinging me! (list of commands: ${config.prefix}help)`, replyMessageIds: [message.id] });
} else if (message.mentions
&& message.mentions.users?.find(user => user.id === client.user?.id)
) {
void message.createMessage({
content: `haha, stop pinging me! (list of commands: ${config.prefix}help)`,
replyMessageIds: [message.id]
});
}
});

// Function used to assign a default settings object to a guild ID
function setGuildSettings(guildID) {
const settings = {
cmdnotfound: true
not_found: true
}
guildSettingsMap.set(guildID, settings);
}
Expand Down
71 changes: 53 additions & 18 deletions examples/snipe.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// This script is only working in current development builds due to Message.oldContent changes.
// Importing Client,
// Message is also imported as we'll be using it later on.
const { Client, Message } = require("touchguild"); // JavaScript/TS (CommonJS)
// TypeScript/JavaScript (ES): import { Client, Message } from "touchguild";

// Importing TouchGuild.Client
const { Client, Message } = require("touchguild");

// Creating client & connecting.
// Constructing Application Client (required)
const client = new Client({
token: "TOKEN"
token: "INSERT TOKEN"
});

// Listen for the Client 'ready' state to be emitted.
client.on("ready", () => {
console.log("Ready as", client.user?.username);
});

// Listen for errors
client.on("error", (err) => {
console.log(err);
});
Expand All @@ -20,26 +22,58 @@ client.on("error", (err) => {
const lastDeletedMessage = new Map();
const lastEditedMessage = new Map();

// Standard command handler, message detection.
// Listen for new messages sent by users
// We'll be building through conditional statements a way to
// detect commands in messages and perform the actions we want
// according to the command that is requested.
client.on("messageCreate", (message) => {
// Split on space the message content to separate each word
// and detect later on the command in the message as a first argument.
const args = message.content?.split(" "); // array of args.

message.content = message.content?.toLowerCase();
// Make the whole message content in lowercase to detect the command in proper conditions
message.content = message.content?.toLowerCase() ?? null;

// Detect !snipe CHANNEL_ID command in chat
if (message.content?.startsWith("!snipe") && args?.[1]) {
if (!lastDeletedMessage.has(args?.[1])) return message.createMessage({ content: `No deleted message detected for: *${args?.[1]}*` });
return message.createMessage({ content: `Last deleted message content: ${lastDeletedMessage.get(args?.[1])}` });
} else if (message.content == "!snipe") {
if (!lastDeletedMessage.has(message.channelID)) return message.createMessage({ content: "No deleted message detected for the moment." });
return message.createMessage({ content: `Last deleted message content: ${lastDeletedMessage.get(message.channelID)}` });
if (!lastDeletedMessage.has(args?.[1]))
return message.createMessage({
content: `No deleted message detected for: *${args?.[1]}*`
});
return message.createMessage({
content: `Last deleted message content: ${lastDeletedMessage.get(args?.[1])}`
});
} else if (message.content == "!snipe") { // Detect !snipe command in chat
// Check if map has this message ID
if (!lastDeletedMessage.has(message.channelID))
return message.createMessage({
content: "No deleted message detected for the moment."
});
// Send message with old message content if it is stored
return message.createMessage({
content: `Last deleted message content: ${lastDeletedMessage.get(message.channelID)}`
});
}

// Detect !editsnipe CHANNEL_ID command in chat
if (message.content?.startsWith("!editsnipe") && args?.[1]) {
if (!lastEditedMessage.has(args?.[1])) return message.createMessage({ content: `No edited message detected for: *${args?.[1]}*` });
return message.createMessage({ content: `Last edited message content: ${lastEditedMessage.get(args?.[1])}` });
} else if (message.content == "!editsnipe") {
if (!lastEditedMessage.has(message.channelID)) return message.createMessage({ content: "No edited message detected for the moment." });
return message.createMessage({ content: `Last edited message content: ${lastEditedMessage.get(message.channelID)}` });
if (!lastEditedMessage.has(args?.[1]))
return message.createMessage({
content: `No edited message detected for: *${args?.[1]}*`
});
return message.createMessage({
content: `Last edited message content: ${lastEditedMessage.get(args?.[1])}`
});
} else if (message.content == "!editsnipe") { // Detect !editsnipe command in chat
// Check if map has this message ID
if (!lastEditedMessage.has(message.channelID))
return message.createMessage({
content: "No edited message detected for the moment."
});
// Send message with old message content if it is stored
return message.createMessage({
content: `Last edited message content: ${lastEditedMessage.get(message.channelID)}`
});
}
});

Expand All @@ -55,4 +89,5 @@ client.on("messageDelete", (message) => {
} else return;
});

// Connect the Client to the API (required)
client.connect();

0 comments on commit 24e34af

Please sign in to comment.