Created
March 28, 2022 21:18
-
-
Save gewenyu99/3087880148d51c9215da9e30d40b03bd to your computer and use it in GitHub Desktop.
Appwrite Cloud Function as a simple Discord Moderation Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sdk = require("node-appwrite"); | |
const { Client, Intents, MessageEmbed } = require('discord.js'); | |
let client = null; | |
let appwrite = null; | |
let database = null; | |
// This is the entry point for our cloud function | |
module.exports = async function (req, res) { | |
if (client) { | |
res.send("Already initialized"); | |
return | |
} | |
initClient(req); | |
res.send("Initialization Complete!"); | |
}; | |
// This is ran once to init the Discord.js client. | |
function initClient(req) { | |
appwrite = new sdk.Client(); | |
appwrite = appwrite | |
.setEndpoint(req.env['APPWRITE_FUNCTION_ENDPOINT']) | |
.setProject(req.env['APPWRITE_FUNCTION_PROJECT_ID']) | |
.setKey(req.env['APPWRITE_FUNCTION_API_KEY']) | |
.setSelfSigned(true); | |
database = new sdk.Database(appwrite); | |
client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); | |
client.once('ready', () => { | |
console.log('Ready!'); | |
}); | |
client.on('messageCreate', async (message) => { | |
// Ignore bots | |
if (message.author.bot) return; | |
let command = message.content.split(' ')[0]; | |
let params = message.content.split(' ').slice(1); | |
// Our first command | |
switch (command) { | |
case '!hello': | |
message.channel.send('world!'); | |
break; | |
case '!warn': | |
if (message.member.permissions.has('MANAGE_MESSAGES')) { | |
let member = message.mentions.members.first().user; | |
let reason = params.slice(1).join(' ').trim(); | |
let warnings = 0; | |
if (!member || !reason) { | |
message.channel.send("The command should be formatted as: `!warn <@member> <reason>`"); | |
return | |
} | |
try { | |
let { documents, total } = await database.listDocuments(req.env['COLLECTION_ID'], [sdk.Query.equal("member", member.id)], 1); | |
if (total < 1) { | |
await database.createDocument(req.env['COLLECTION_ID'], 'unique()', | |
{ | |
"member": member.id, | |
"warnings": 1, | |
"reasons": [reason] | |
}); | |
warnings = 1; | |
} | |
else { | |
id = documents[0]["$id"] | |
warnings = documents[0]["warnings"] + 1; | |
const reasons = documents[0]["reasons"].concat([reason]); | |
await database.updateDocument(req.env['COLLECTION_ID'], id, { | |
"warnings": warnings, | |
"reasons": reasons | |
}); | |
} | |
} | |
catch (e) { | |
message.channel.send("Something broke when logging warning to Appwrite!"); | |
console.log(e); | |
return; | |
} | |
let warnEmbed = new MessageEmbed() | |
.setColor('#ff0000') | |
.setTitle('Warn') | |
.setDescription(`${member.username} has been warned for ${reason}. ${member.username} has been warned ${warnings} times!`); | |
message.channel.send({ embeds: [warnEmbed] }); | |
if (warnings >= 3) { | |
message.mentions.members.first().ban(); | |
message.channel.send(`${member.username} has been banned for 3 warnings!`); | |
} | |
} | |
else { | |
message.channel.send("You don't have permission to use !warn"); | |
} | |
case '!record': | |
if (message.member.permissions.has('MANAGE_MESSAGES')) { | |
let member = message.mentions.members.first().user; | |
let warnings = 0; | |
if (!member) { | |
message.channel.send("The command should be formatted as: `!record <@member>`"); | |
return | |
} | |
try { | |
let { documents, total } = await database.listDocuments(req.env['COLLECTION_ID'], [sdk.Query.equal("member", member.id)], 1); | |
if (total < 1) { | |
warnings = 0 | |
} | |
else { | |
id = documents[0]["$id"] | |
warnings = documents[0]["warnings"]; | |
} | |
} | |
catch (e) { | |
message.channel.send("Something broke while trying to fetch past warnings from Appwrite!"); | |
return; | |
} | |
let recordEmbed = new MessageEmbed() | |
.setColor('#00ff00') | |
.setTitle('Warning Record') | |
.setDescription(`${member.username} has been warned ${warnings} times!`); | |
message.channel.send({ embeds: [recordEmbed] }); | |
} | |
else { | |
message.channel.send("You don't have permission to use !record"); | |
} | |
} | |
}); | |
client.login(req.env['DISCORD_TOKEN']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment