-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
122ab36
commit 3e3cb2a
Showing
6 changed files
with
451 additions
and
13 deletions.
There are no files selected for viewing
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
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
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(); | ||
}, | ||
}; |
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
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; |
Oops, something went wrong.