Skip to content

Commit

Permalink
Paste command (#19)
Browse files Browse the repository at this point in the history
* feat: Create paste command

- created GitHub gist for the code

* enhance: Delete original message

- also added creation-time to embed

* Minor change for some old code.

Co-authored-by: Utkarsh <[email protected]>
  • Loading branch information
hemant2132 and utkarsh261 authored Jan 9, 2021
1 parent 122ab36 commit 3e3cb2a
Show file tree
Hide file tree
Showing 6 changed files with 451 additions and 13 deletions.
12 changes: 6 additions & 6 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bot.on("guildMemberAdd", async (member) => {
.setColor("#176ffc")
.setTitle(`Yay! ${name} you made it to KPH discord Server `)
.setDescription(
`I am your friendly bot written in Javascript, Feel free to tell us more about yourself.\n *If* you wish to be identified as JIITian, send !verify in #general :D.`
`I am your friendly bot written in Javascript, Feel free to tell us more about yourself.\n *If* you wish to be identified as JIITian, send !verify in the #verify channel :D.`
)
.setFooter("Use !help command to know more about me ");
channel.send(welcomeEmbed);
Expand All @@ -34,8 +34,8 @@ bot.on("guildMemberAdd", async (member) => {
}
});

bot.on("ready", async () => {
await getFiles("./commands")
bot.on("ready", () => {
getFiles("./commands")
.then((files) => {
for (const file of files) {
const command = require(file);
Expand All @@ -51,15 +51,15 @@ setInterval(() => remind(bot), 3000000);
bot.on("message", (message) => {
if (message.channel.type === "dm") return;

const args = message.content.trim().split(/\r\n|\r|\n| +/);
const args = message.content.trim().split(/\r\n|\r|\n| +/); // removes any whitespace in the message
const command = args.shift().toLowerCase();

if (message.channel.id === process.env.VERIFY_CHANNEL_ID) {
if (command === "!verify") verify(bot, message.author);
return;
}

// If a command is not present , log the default message
// If a command is not present, log the default message
if (!bot.commands.has(command)) {
if (command[0] === "!") bot.commands.get("!invalid").execute(message, args);
return;
Expand All @@ -70,7 +70,7 @@ bot.on("message", (message) => {
bot.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply("there was an error trying to execute that command!");
message.reply("There was some error in executing that command! 🙁");
}
});

Expand Down
68 changes: 68 additions & 0 deletions commands/misc/paste.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const octokit = require("../../octokit");

const codeObj = {
filename: "",
code: "",
};

const compute = (args) => {
const codePos = args.indexOf("\n");

if (codePos === -1) return false;

codeObj.filename = "";
for (
let i = codePos - 1;
args[i] !== " " && args.substring(0, i + 1) !== "!paste";
--i
)
codeObj.filename += args[i];

codeObj.filename = codeObj.filename.split("").reverse().join("");
codeObj.code = args.substring(codePos + 1);

return codeObj.code !== "";
};

module.exports = {
name: "!paste",
description: "Paste code and get a link for it",
usage: "```!paste\n\nFormat:\n!paste <filename>\n<code>```",
execute: async (message, recievedArgs) => {
const args = String(message.content.trim());

if (!compute(args)) {
message.channel.send(
"Wrong format! Make sure the code is not empty. Use !help paste to know about usage."
);
return;
}

await octokit.gists
.create({
public: false,
files: {
[codeObj.filename]: { content: codeObj.code },
},
})
.then((gist) => {
const time = new Date();
const successMessage = {
content: `<@${message.author.id}> the code is ready! 😀`,
embed: {
title: "Find it here!",
url: `${gist.data.html_url}`,
color: 4045991,
description: `Created at ${time.toString()}.`,
},
};
message.channel.send(successMessage);
})
.catch((err) => {
message.reply("There was some error. 🙁");
console.log(err);
});

message.delete();
},
};
40 changes: 40 additions & 0 deletions octokit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { Octokit } = require("@octokit/rest");
const { retry } = require("@octokit/plugin-retry");
const { throttling } = require("@octokit/plugin-throttling");

const MyOctokit = Octokit.plugin(retry, throttling);

const octokit = new MyOctokit({
auth: process.env.GITHUB_TOKEN,
userAgent: "KLE-bot v1.0.0",
log: {
debug: () => {},
info: () => {},
warn: console.warn,
error: console.error,
},
throttle: {
onRateLimit: (retryAfter, options) => {
octokit.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);

if (options.request.retryCount === 0) {
// only retries once
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
octokit.log.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
},
},
retry: {
doNotRetry: ["429"],
},
});

module.exports = octokit;
Loading

0 comments on commit 3e3cb2a

Please sign in to comment.