Add qrcode command
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
"version": "1.0.0",
|
||||
"description": "Discord utility bot",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"start": "node --no-warnings src/index.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.0xfffe.org/frostbide/frostbot"
|
||||
@@ -19,6 +22,7 @@
|
||||
"dotenv": "^16.4.7",
|
||||
"fuzzball": "^2.1.3",
|
||||
"js-yaml": "^4.1.0",
|
||||
"node-fetch": "^2.7.0"
|
||||
"node-fetch": "^2.7.0",
|
||||
"qrcode": "^1.5.4"
|
||||
}
|
||||
}
|
||||
|
||||
76
src/commands/slash/qrcode.js
Normal file
76
src/commands/slash/qrcode.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const { SlashCommandBuilder, EmbedBuilder } = require('@discordjs/builders');
|
||||
const { AttachmentBuilder } = require('discord.js');
|
||||
const QRCode = require('qrcode');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('qrcode')
|
||||
.setDescription('QR-Code generator')
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName('data')
|
||||
.setDescription('The data to be encoded in the qr-code')
|
||||
.setRequired(true)
|
||||
)
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName('error-correction')
|
||||
.setDescription(
|
||||
'The error correction level of the qr-code (Default: Medium)'
|
||||
)
|
||||
.addChoices(
|
||||
{ name: 'Low', value: 'L' },
|
||||
{ name: 'Medium', value: 'M' },
|
||||
{ name: 'Quartile', value: 'Q' },
|
||||
{ name: 'High', value: 'H' }
|
||||
)
|
||||
.setRequired(false)
|
||||
)
|
||||
.addIntegerOption((option) =>
|
||||
option
|
||||
.setName('version')
|
||||
.setDescription('Version of qr-code')
|
||||
.setMinValue(1)
|
||||
.setMaxValue(40)
|
||||
.setRequired(false)
|
||||
),
|
||||
async run(context) {
|
||||
const interaction = context.interaction;
|
||||
const { options } = interaction;
|
||||
|
||||
const rawText = options.getString('data');
|
||||
const version = options.getInteger('version');
|
||||
const errorCorrectionLevel = options.getString('error-correction') || 'M';
|
||||
|
||||
await QRCode.toDataURL(
|
||||
rawText,
|
||||
{
|
||||
errorCorrectionLevel,
|
||||
version,
|
||||
},
|
||||
function (err, dataUrl) {
|
||||
if (err) {
|
||||
return interaction.reply({
|
||||
content: 'Error',
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
|
||||
const generated = new AttachmentBuilder(
|
||||
Buffer.from(dataUrl.split(',')[1], 'base64'),
|
||||
{ name: 'qrcode.png' }
|
||||
);
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle('QR Code')
|
||||
.setDescription(`Input data: \`${rawText}\``)
|
||||
.setImage('attachment://qrcode.png')
|
||||
.setColor(0x009bc2);
|
||||
|
||||
interaction.reply({
|
||||
files: [generated],
|
||||
embeds: [embed],
|
||||
});
|
||||
}
|
||||
);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user