jonathonor / trialbot Goto Github PK
View Code? Open in Web Editor NEWA bot to give temporary access to a role on discord. It gives the ability to expire a users role after a set amount of time.
Home Page: https://www.jonsbots.com/trialbot
A bot to give temporary access to a role on discord. It gives the ability to expire a users role after a set amount of time.
Home Page: https://www.jonsbots.com/trialbot
]
}
PS C:\Users\PC\OneDrive\Masaüstü\TimerBot\trialbot> node register.js
Started refreshing application (/) commands.
(node:7604) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use node --trace-warnings ...
to show where the warning was created)
DiscordAPIError[0]: 404: Not Found
at handleErrors (file:///C:/Users/PC/OneDrive/Masa%C3%BCst%C3%BC/TimerBot/trialbot/node_modules/@discordjs/rest/dist/index.mjs:588:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async SequentialHandler.runRequest (file:///C:/Users/PC/OneDrive/Masa%C3%BCst%C3%BC/TimerBot/trialbot/node_modules/@discordjs/rest/dist/index.mjs:969:23)
at async SequentialHandler.queueRequest (file:///C:/Users/PC/OneDrive/Masa%C3%BCst%C3%BC/TimerBot/trialbot/node_modules/@discordjs/rest/dist/index.mjs:810:14)
at async REST.request (file:///C:/Users/PC/OneDrive/Masa%C3%BCst%C3%BC/TimerBot/trialbot/node_modules/@discordjs/rest/dist/index.mjs:1335:22)
at async file:///C:/Users/PC/OneDrive/Masa%C3%BCst%C3%BC/TimerBot/trialbot/register.js:40:5 {
requestBody: {
files: undefined,
json: [ [SlashCommandBuilder], [SlashCommandBuilder] ]
},
rawError: { message: '404: Not Found', code: 0 },
code: 0,
status: 404,
method: 'PUT',
url: 'https://discord.com/api/v9/applications/1095615764507148340 / 1095615764507148340/commands'
}
PS C:\Users\PC\OneDrive\Masaüstü\TimerBot\trialbot> node trialbot-lowdb.js
(node:2828) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use node --trace-warnings ...
to show where the warning was created)
C:\Users\PC\OneDrive\Masaüstü\TimerBot\trialbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:250
throw new DiscordjsError(unrecoverableErrorCodeMap[error.code]);
^
Error [DisallowedIntents]: Privileged intent provided is not enabled or whitelisted.
at WebSocketManager.createShards (C:\Users\PC\OneDrive\Masaüstü\TimerBot\trialbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:250:15)
at async Client.login (C:\Users\PC\OneDrive\Masaüstü\TimerBot\trialbot\node_modules\discord.js\src\client\Client.js:226:7) {
code: 'DisallowedIntents'
}
I fixed and you can update your repo, and add Discord.js in package.json. Thanks.
Working codes;
`/*
expireBot, a super simple bot that gives you the ability to give a user a role for a
certain number of days, after the time is reached, the role is removed.
*/
var mysql = require('promise-mysql');
var schedule = require('node-schedule');
const Discord = require('discord.js');
const client = new Discord.Client();
var config = require('./config.json');
var pool = mysql.createPool({
connectionLimit: 100, //important
host: config.dbHost,
user: config.dbUsername,
password: config.dbPassword,
database: config.dbName,
debug: false
});
var connection;
client.on('ready', () => {
console.log('expireBot started.');
mysql.createConnection({
host: config.dbHost,
user: config.dbUsername,
password: config.dbPassword
}).then(conn => {
connection = conn;
return connection.query('SHOW DATABASES LIKE ?;', config.dbName);
}).then(rows => {
if (!rows[0]) {
console.log('database does not exist')
}
});
});
client.on('message', msg => {
if (msg.content.startsWith('!role')) {
if (verifyUser(msg.author.id)) {
let member = msg.mentions.members.first();
let role = msg.mentions.roles.first().id;
let days = parseInt(msg.content.split(" ")[3]);
if (Number.isInteger(days)) {
addRole(member, role, days);
} else {
const guild = client.guilds.cache.find(guild => guild.id === config.serverId);
const logChannel = guild.channels.cache.find(channel => channel.id === config.logChannelId);
logChannel.send(days + ' is not a valid number.');
}
}
}
});
addRole = (member, roleId, amountOfDays) => {
const guild = client.guilds.cache.find(guild => guild.id === config.serverId);
const logChannel = guild.channels.cache.find(channel => channel.id === config.logChannelId);
const roleToAdd = guild.roles.cache.find(r => r.id === roleId);
let checkSql = 'SELECT expires FROM users WHERE id = ? AND role = ?';
let checkParams = [member.user.id, roleId];
pool.getConnection().then(connection => {
connection.query(checkSql, checkParams).then(rows => {
connection.release();
if (rows[0]) {
let dateToAddTo = new Date(rows[0].expires);
updateExpirationDate(member.user.id, roleId, amountOfDays, dateToAddTo);
logChannel.send('Added ' + amountOfDays + ' days to ' + member.user.username + '\s subscription.');
} else {
insertMemberIntoDb(member.user.id, roleId, amountOfDays)
member.roles.add(roleToAdd).catch(err => console.log(err));
logChannel.send('Added "' + roleToAdd.name + '" role and ' + amountOfDays + ' days to ' + member.user.username + '\s subscription.');
}
});
});
}
updateExpirationDate = (memberId, roleId, amountOfDays, dateToAddTo) => {
let updateSql = 'UPDATE users SET expires = ? WHERE id = ? AND role = ?';
let expireDate = addDays(amountOfDays, dateToAddTo);
let updateParams = [expireDate, memberId, roleId];
pool.getConnection().then(connection => {
connection.query(updateSql, updateParams).then(rows => {
connection.release();
});
});
}
insertMemberIntoDb = (memberId, roleId, amountOfDays) => {
let insertSql = 'INSERT INTO users (id, role, expires) VALUES (?, ?, ?);';
let expireDate = addDays(amountOfDays);
let insertParams = [memberId, roleId, expireDate];
pool.getConnection().then(connection => {
connection.query(insertSql, insertParams).then(rows => {
connection.release();
});
});
}
addDays = (days, date) => {
var result = date ? new Date(date) : new Date();
result.setDate(result.getDate() + days);
return result;
}
verifyUser = (id) => {
const guild = client.guilds.cache.find(guild => guild.id === config.serverId);
let member = guild.members.cache.find(member => member.id === id);
return member.roles.cache.find(role => role.name === config.commanderRole);
}
removeRoles = () => {
let guild = client.guilds.cache.find(guild => guild.id === config.serverId);
let rowsToRemoveSql = 'SELECT * FROM users WHERE expires < CURRENT_TIMESTAMP()';
let logChannel = guild.channels.cache.find(channel => channel.id === config.logChannelId);
pool.getConnection().then(connection => {
connection.query(rowsToRemoveSql).then(rows => {
connection.release();
if (rows.length > 0) {
rows.forEach(row => {
let guild = client.guilds.cache.find(guild => guild.id === config.serverId);
let role = guild.roles.cache.find(r => r.id === row.role);
let member = guild.members.cache.find(mem => mem.id === row.id);
member.removeRole(role);
logChannel.send('Removed "' + role.name + '" from ' + member.user.username);
});
}
});
});
}
deleteExpiredUsers = () => {
let removeSql = 'DELETE FROM users WHERE expires < CURRENT_TIMESTAMP()';
pool.getConnection().then(connection => {
connection.query(removeSql).then(() => {
connection.release();
});
});
}
schedule.scheduleJob('0 */8 * * *', () => {
removeRoles();
deleteExpiredUsers();
});
client.login(config.token);`
I have followed the steps.The issue is I have a channel name get-trial, which has a get trial button, it gives error, This interaction failed.
I have followed all the steps but I dont know where to run the add command. I nee it to be behind the get trial button so that everytime a user clicks this button it gives him the role(already created ) for 6 hours.
Morever the plus trial role is in 2nd number because at 1st, admin is pinned and cant be removed.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.