Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
devuuuxd committed Aug 4, 2024
1 parent 39058ef commit b6fcc83
Show file tree
Hide file tree
Showing 5 changed files with 987 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
10 changes: 5 additions & 5 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
token: "", // Add your bot's token here
channel_name: "@devuuuu_xd", // Specify your youtube channel's name here (youtube)
role_id:"", //if of role which you wanted to be given
keywords: "SUBSCRIBED", // Specify the keywords for analyzing the image
save_data:"false" // do you want to save data in subscriber.json for yes say "true" for no say "false"
token: "", // Add your bot's token here (REQUIRED)
channel_name: "@devuuuu_xd", // Specify your youtube channel's name here (REQUIRED)
role_id:"", //if of role which you wanted to be given (OPTIONAL)
keywords: "", // Specify the keywords for analyzing the image (OPTIONAL)
save_data:"true" // do you want to save data in subscriber.json for yes say "true" for no say "false" (OPTIONAL)
};
Binary file added eng.traineddata
Binary file not shown.
62 changes: 40 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ const fetch = require('node-fetch');
const fs = require('fs');
const config = require("./config.js");

// Getting Config
if (!config.token) {
console.error('Error: Bot token is required in config.js');
process.exit(1);
}
if (!config.channel_name) {
console.error('Error: Channel name is required in config.js');
process.exit(1);
}

const role_id = config.role_id || null;
const keywords = config.keywords || null;
const save_data = config.save_data || 'false';



// Creating a Client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
GatewayIntentBits.GuildMembers
]
});

client.once(Events.ClientReady, async readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);


// Register slash commands
const commands = [
new SlashCommandBuilder()
.setName('verify')
Expand Down Expand Up @@ -48,7 +61,8 @@ client.once(Events.ClientReady, async readyClient => {
});


// check if a user is already verified

// Check if a user is already verified
const isUserVerified = (userId) => {
if (!fs.existsSync('subscriber.json')) return false;

Expand All @@ -57,6 +71,7 @@ const isUserVerified = (userId) => {
};



// Command handling
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isCommand()) return;
Expand All @@ -66,21 +81,21 @@ client.on(Events.InteractionCreate, async interaction => {
if (commandName === 'verify') {
const member = interaction.member;

await interaction.deferReply({ephemeral: true}); // Defer the reply as image processing might take some time
await interaction.deferReply({ ephemeral: true });
if (!member) {
await interaction.followUp({ content: 'Member not found.', ephemeral: true });
return;
}


// check if the user is already verified
// Check if the user is already verified
if (isUserVerified(member.user.id)) {
await interaction.followUp({ content: 'You are already verified.', ephemeral: true });
return;
}

const image = interaction.options.getAttachment('image');

if (!image || !image.url) {
await interaction.followUp({ content: 'Please provide a valid image.', ephemeral: true });
return;
Expand All @@ -91,7 +106,7 @@ client.on(Events.InteractionCreate, async interaction => {
const allowedExtensions = ['jpeg', 'png', 'webp', 'gif'];
const url = new URL(image.url);
const fileExtension = url.pathname.split('.').pop().toLowerCase();

console.log(`File extension: ${fileExtension}`);

if (!allowedExtensions.includes(fileExtension)) {
Expand All @@ -100,6 +115,7 @@ client.on(Events.InteractionCreate, async interaction => {
}



// Getting the Image
try {
const response = await fetch(image.url);
Expand All @@ -111,36 +127,38 @@ client.on(Events.InteractionCreate, async interaction => {
const processedImage = await sharp(buffer)
.resize({ width: 1000 })
.toBuffer();


// Use Tesseract to extract text
const { data: { text } } = await Tesseract.recognize(processedImage);


// Convert extracted text and channel name to lowercase for case-insensitive comparison
const extractedTextLower = text.toLowerCase();
const channelNameLower = config.channel_name.toLowerCase();

console.log(`Extracted text: ${text}`);

console.log(`Extracted text: ${text}`);

// Check if the extracted text contains the channel_name
let containsChannelName = extractedTextLower.includes(channelNameLower);


// If keywords are provided, also check for them
if (config.keywords) {
const keywordsArray = config.keywords.split(',').map(keyword => keyword.trim().toLowerCase());
if (keywords) {
const keywordsArray = keywords.split(',').map(keyword => keyword.trim().toLowerCase());
containsChannelName = containsChannelName || keywordsArray.some(keyword => extractedTextLower.includes(keyword));
}

if (containsChannelName) {
await member.roles.add(config.role_id);
if (role_id) {
await member.roles.add(role_id);
}
await interaction.followUp({ content: `Thanks for subscribing to ${config.channel_name}`, ephemeral: true });




// Save user data if save_data is true
if (config.save_data === 'true') {
if (save_data === 'true') {
const userData = {
username: member.user.username,
id: member.user.id,
Expand All @@ -155,10 +173,10 @@ client.on(Events.InteractionCreate, async interaction => {
fs.writeFileSync('subscriber.json', JSON.stringify(subscribers, null, 2));
}
} else {
await interaction.followUp({
content: `You haven't subscribed to ${config.channel_name} or if this is an error please send a cropped image like attached below!`,
files: ["https://i.ibb.co/rQdzcbT/image.png"],
ephemeral: true
await interaction.followUp({
content: `You haven't subscribed to ${config.channel_name} or if this is an error please send a cropped image like attached below!`,
files: ["https://i.ibb.co/rQdzcbT/image.png"],
ephemeral: true
});
}
} catch (error) {
Expand All @@ -175,4 +193,4 @@ client.login(config.token).catch(err => {
/* https://www.youtube.com/@devuuu_xd (YOUTUBE)
/* https://github.com/devuuuxd (GITHUB)
/* MAKE SURE TO GIVE ME CREDITS 😼😼
/*/
/*/
Loading

0 comments on commit b6fcc83

Please sign in to comment.