Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Update. #3

Open
wants to merge 32 commits into
base: config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2cdeb26
Create discord.json
supreen Jun 12, 2020
59df63c
Create index.html
supreen Jun 12, 2020
e38e120
Update README.md
supreen Jun 12, 2020
b27d018
Update README.md
supreen Jun 12, 2020
bbedf3f
Update README.md
supreen Jun 12, 2020
4c1ba00
Updated
supreen Jun 28, 2020
f7e2f59
Update discord.json
supreen Jun 28, 2020
7389c9c
Update README.md
supreen Jun 28, 2020
184483a
Update index.js
supreen Jul 10, 2020
349c725
update
supreen Aug 12, 2021
4f7e068
Create README.md
supreen Aug 12, 2021
2f0d03b
Update README.md
supreen May 27, 2022
2d3302f
Update README.md
supreen May 27, 2022
1aedfbb
Update README.md
supreen Jun 22, 2022
0e9f1a1
Update README.md
supreen Jun 22, 2022
67f80b3
adding images to the readme
supreen Jun 22, 2022
8d22675
added image for readme
supreen Jun 22, 2022
18836ac
Update README.md
supreen Jun 22, 2022
9e7006e
Update README.md
supreen Jun 22, 2022
0b8578e
Delete asd.jpg
supreen Jun 22, 2022
a9e780b
Update README.md
supreen Jun 22, 2022
c4624d2
Update README.md
supreen Jun 22, 2022
cee453c
Update README.md
supreen Jun 22, 2022
fb094dc
added image for readme
supreen Jun 22, 2022
31c53d1
Update README.md
supreen Jun 22, 2022
d1359f4
Updated further details and images
supreen Jun 22, 2022
5b0ba5c
Update README.md
supreen Jun 22, 2022
25803b0
added spreadsheet link
supreen Jun 22, 2022
c2df8d0
added demo link
supreen Jun 22, 2022
7de22db
Update README.md
supreen Jun 22, 2022
009c4d9
Update README.md
supreen Jun 13, 2023
696aa9f
Update README.md
supreen Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Image/highrole.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Image/permission.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 118 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,118 @@
# form2role-bot
Discord bot that automatically assigns roles to users found in a Google Sheet
Form2Role-Bot:
<h2>About</h2>
<p>This is the simplified version of <a href="https://github.com/talios0" target="_blank">talios0's</a> and <a href="https://github.com/The24thDS" target="_blank">The24thDS's </a> bot. The bot assigns the roles (according to the 2nd,3rd,4th and 5th column of the spreadsheet) to the usernames found in a 1st column of Google Sheet. Made possible by the amazing Discord.js library.
Please note that the bot start detecting the data from 2nd row of spreadsheet. </p>
<h2>Motivation</h2>
<p>Did you ever want to add some kind of authentication to your Discord server? Maybe you want only people with a certain email domain to have access to some channels. Use a form to collect the required data and then set up this bot to automatically assign roles to everyone found in the Google Sheet.</p>

<h2>Required fields</h2>
<p>The only field that you must have in your Google Form and Google Sheet is the DiscordID. Your DiscordID it's made up of your username and the discriminator, and it looks like this: SomeSexyUsername#4565. The bot needs it to identify users and assign roles to them.(When you selected "UN" in discord.config)</p>
<p>With new update, now you can also add USERID which is unique id and can be found by right clicking on the username and clicking on "COPY ID" it will be something like "696051693519503421" (When you selected "UI" in discord.config).</p>

<h2>How to get your Google details<h2>
<h3>API key</h3>
<ul>
<li>Go to https://console.developers.google.com</li>
<li>Login and Agree to the Terms of Service</li>
<li>Create a Project by pressing "Create Project" on the right-hand side</li>
<li>Click "Create", give the project a name and then "Create" again.</li>
<li>Once the dashboard loads, click on "ENABLE APIS AND SERVICES" in blue text.</li>
<li>Search for "Sheets" and click on "Google Sheets API" and then "ENABLE"</li>
<li>Click on "Credentials" on the left side of the page.</li>
<li>Select "Create credentials" and then "API Key"</li>
</ul>
<h2>Spreadsheet ID</h2>
<ul>
<li>Go to your Google Sheets document and make sure it is accessible to anyone with the link, view-only.</li>
<li>The id is the part from your URL that comes after /d/. If the url is https://docs.google.com/spreadsheets/d/hjgt6sdBhbHdDssfjGk/edit?usp=sharing, your id is hjgt6sdBhbHdDssfjGk</li>
</ul>
<h2>Range</h2>

<p>If the column that contains the Discord IDs is B and the IDs start from row 2 then the range value (don't touch it, if you don't know what you are doing)</p>
<h2>How to setup a Discord bot</h2>
<p>You will need a bot token, to generate one follow these steps:</p>

<ul>
<li>Go to https://discordapp.com/developers and login</li>
<li>Create an application and give it a name and a description</li>
<li>Select "Save Changes" at the bottom right and then navigate to the "Bot" section on the left-hand side.</li>
<li>There, create a bot user by clicking on "Add Bot". After confirming, reveal the token.</li>
</ul>
<h3>Invite the bot to your discord server:</h3>
<ul>
<li>Go to OAuth2 section of your developer application</li>
<li>Under SCOPES check bot</li>
<li>Under BOT PERMISSIONS check Manage Roles</li>
<li>Now go to the url generated in the SCOPES section and invite the bot to your server</li>
<li>Make sure the bot role is above the roles it has to assign</li>
</ul>
<h3>Configuration steps</h3>
<ul>
<li>Install Node.JS</li>
<li>Download the latest bot version from the release tab and unzip it</li>
<li>Go to the config folder, copy and paste the config files</li>
<li>Open discord.json and complete your API key, your Spreadsheet id and Token (See How to get your Google details). All of these are strings, so make sure they are enclosed in quotation marks. (don't mess with empty arrays and variable such as usernamesp and roles)</li>

<li>Go back to the root folder, open a terminal here and run these commands: npm install, wait for it to finish then run npm start.</li>
<li> Run "npm install discord.js" , "npm install express" and "npm install request" (if required) </li>
<li>Congratulations, if you did everything correctly you now have Form2Role bot running.</li>
</ul>
<h3>More info</h3>
<ul>
<li>The bot checks the sheet for new users and roles every minute. However you can change that in the code. i.e refresh rate in discord.json. it is in miliseconds</li>

<li>The bot only supports upto 4 roles in column 2, 3, 4 ,5 </li>

<li>Once you enter a "sexyusername#1111" in the spreadsheet, you can no longer control its roles from dicord. the bot will revert the roles back to the roles mentioned in the spreadsheet. </li>


<li>Future updates will include mainly bug fixes.</li>
<li>Contributions are highly apreciated.</li>
</ul>


<h3>New updates on 28/06/2020</h3>

<li>Now the bot can detect userid instead of username. change the value of "usernameorid" in discord.json. i.e ["username":"UN"] for detecting username and ["username":"UI"] for detecting userid </li>
<li>We just realized that bot was throwing "missing permission" error when it was trying to remove unremovable roles. there now you can put unremoveable roles or the roles you dont want this bot to remove in "unremoveableroles" in discord.json. i.e "unremoveableroles":["bot","serverboost"] </li>
<li>The bot was doing nothing when all roles were removed, it detects and removes all roles now</li>
<li>Now it throws time in console when the bot assign or remove roles of a user</li>



<h2>New Information updates on 22/06/2022</h3>
<br>
<h3>Always add "@everyone" and other unremovable roles in unremovable roles under config/discord.json </h3>
<br>
<h3>You should manually give more permission to your bot roles as shown in the picture below </h3>
<li>Right click on the server "icon"</li>
<li>Click on "Server Setting" from drop menu"</li>
<li>Click "Roles" from the drop menu of "server settings</li>
<li>Click "Roles" menu click on the "role" assigned to your bot</li>
<li>Click "Roles" menu click on the "role" assigned to your bot</li>
<li>Go to "Permissions" Tab and give all permissions to your bot</li>

![Image of Permission](https://github.com/supreen/form2role-bot/blob/master/Image/permission.jpg?raw=true)
<br>
<h2>Your bot role much be higher than another role as shown in the picture below </h2>
<li>Right click on the server "icon"</li>
<li>Click on "Server Setting" from drop menu"</li>
<li>Click "Roles" from the drop menu of "server settings</li>
<li>drag the bot role above all roles</li>
![Image of higher role](https://github.com/supreen/form2role-bot/blob/master/Image/highrole.JPG?raw=true)
<br>


<h3>New demo added on 22/06/2022</h3>
<li>Spreadsheet link:https://docs.google.com/spreadsheets/d/1p1GiIqUUy2b-HcsaCJFHu_ZYQ2nWKZ2zGXmEmu9RrNE/edit#gid=0 </li>
<li>Discord chaneel: https://discord.com/channels/715245634218885131/715245634898100256</li>

<h3>Free free to edit the roles preferable just checking and unchecking the roles. please be cautious, a wrong move can crash the bot.</h3>



<h3>Consider buying me a coffee, if you loved my work.</h3>
<li>https://www.buymeacoffee.com/suprin

<h3>If you are still finding it hard, Hire me</h3>
<li>https://www.fiverr.com/supreen
13 changes: 13 additions & 0 deletions config/discord.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"token": "<your discord token>",
"refreshrate":"60000",
"range":"A2:E",
"spreadsheetId":"<your spreadsheet id>",
"apiKey":"<your api>",
"unremoveableroles":["<the roles(seperated by comma) you believe bot can not remove like server boost role or bot role>"],
"usernameorid":"<Type "UN" here for username and "UI" for userid>",
"refreshrate":"60000",
"roles": [],
"usernamesp": []

}
159 changes: 58 additions & 101 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
var checked;
var checking = [];
const Discord = require("discord.js");
const {token,usernamesp, roles, apiKey, spreadsheetId, range, refreshrate} = require("./config/discord.json");
console.log(token+" "+usernamesp+" "+roles+" "+apiKey+" "+spreadsheetId+" "+range+" "+refreshrate)
const {usernameorid, token,usernamesp, roles, apiKey, spreadsheetId, range, unremoveableroles, refreshrate} = require("./config/discord.json");
const { google } = require("googleapis");

const connection = google.sheets({
Expand Down Expand Up @@ -68,7 +67,7 @@ const fetchRows = async (spreadsheetId, range, sheetsConnection) => {


const therolenames = rows123[1]+","+rows123[2]+","+rows123[3]+","+rows123[4];
console.log(rows123[0].trim()+" "+therolenames.trim());


assignRoles(rows123[0].trim(), therolenames.trim());

Expand Down Expand Up @@ -110,9 +109,10 @@ const assignRoles = async (usernames, roleNames) => {


const guildRoles = client.guilds.array()[0].roles.array();


// deleting unremovable roles from the list
const Roles2 = guildRoles.filter(Role => !unremoveableroles.includes(Role.name));
// * getting Role instances of role names
const removedRoles = guildRoles.filter(Role => unremoveableroles.includes(Role.name));
const Roles = guildRoles.filter(Role => roleNames.includes(Role.name));
//role number of all members in the group

Expand All @@ -121,16 +121,26 @@ const assignRoles = async (usernames, roleNames) => {
// * getting GuildMember instances of usernames and setting roles
guildMembers.forEach(async member => {

const username= member.user.username + "#" + member.user.discriminator;


//check if the first column of spreadsheet matches the userid/username of discord

var UNcheck;

const username = member.user.username + "#" + member.user.discriminator;





if (usernameorid=="UN"){

UNcheck=username

if (usernames.includes(username)) {
}else{

UNcheck=member.id


}


if (usernames.includes(UNcheck)) {

const notAssignedRoles = [];

Expand All @@ -153,30 +163,40 @@ const assignRoles = async (usernames, roleNames) => {
//Check whether the roles (discord) are in roles (spreadsheet)



if (Roles.includes(member.roles.array()[1]) || member.roles.array()[1]==null){
}else{checked="yes" }
if (Roles.includes(member.roles.array()[2]) || member.roles.array()[2]==null){

if (Roles.includes(member.roles.array()[1])|| removedRoles.includes(member.roles.array()[1])|| member.roles.array()[1]==null){

}else{checked="yes" }
if (Roles.includes(member.roles.array()[2]) || removedRoles.includes(member.roles.array()[2]) || member.roles.array()[2]==null){

}else{checked="yes"; }
if (Roles.includes(member.roles.array()[3]) || member.roles.array()[3]==null){
if (Roles.includes(member.roles.array()[3]) || removedRoles.includes(member.roles.array()[3])|| member.roles.array()[3]==null){

}else{checked="yes"; }
if (Roles.includes(member.roles.array()[4]) || member.roles.array()[4]==null){
if (Roles.includes(member.roles.array()[4])|| removedRoles.includes(member.roles.array()[4]) || member.roles.array()[4]==null){

}else{checked="yes"; }


//Check whether the roles (Spreadsheet) are in roles (Discord)


if (member.roles.array().includes(Roles[0]) || Roles[0]==null){
if (member.roles.array().includes(Roles[0])|| removedRoles.includes(Roles[0]) || Roles[0]==null){

}else{checked="yes"; }
if (member.roles.array().includes(Roles[1]) || Roles[1]==null){
if (member.roles.array().includes(Roles[1]) || removedRoles.includes(Roles[1])|| Roles[1]==null){


}else{checked="yes"; }
if (member.roles.array().includes(Roles[2]) || Roles[2]==null){
if (member.roles.array().includes(Roles[2]) || removedRoles.includes(Roles[2]) || Roles[2]==null){

}else{checked="yes"; }
if (member.roles.array().includes(Roles[3]) || Roles[3]==null){
}else{checked="yes"; }


if (member.roles.array().includes(Roles[3])|| removedRoles.includes(Roles[3]) || Roles[3]==null){

}else{checked="yes"; }



if (checked=="yes"){
notAssignedRoles.push(role);
}
Expand All @@ -185,98 +205,35 @@ const assignRoles = async (usernames, roleNames) => {

if (notAssignedRoles.length > 0) {

await member.removeRoles(guildRoles); member.addRoles(Roles);


console.log(
"Assigned " +
notAssignedRoles.map(role => role.name) +
" to " +
username + " on " + new Date().toString()
);
} else {

console.log(username + " already has all the roles assigned");
}
}
});
} catch (err) {
logError(err);
}
};



const assignRoles2 = async (usernames, roleNames,) => {


try {
const guildMembers = client.guilds.array()[0].members.array();

//number of all members in the group


const guildRoles = client.guilds.array()[0].roles.array();


// * getting Role instances of role names
const Roles = guildRoles.filter(Role => roleNames.includes(Role.name));
//role number of all members in the group


roleNames.length=0;
// * getting GuildMember instances of usernames and setting roles
guildMembers.forEach(async member => {


const username = member.user.username + "#" + member.user.discriminator;







if (usernames.includes(username)) {

const notAssignedRoles = [];







Roles.forEach(role => {

//if (!member.roles.array().includes(Roles)) {
//if (!guildRoles.includes(Roles)) {


await member.removeRoles(Roles2); member.addRoles(Roles);






notAssignedRoles.push(role);


});
if (notAssignedRoles.length > 0) {

await member.removeRoles(guildRoles);
await member.addRoles(Roles);

var d = new Date().toString();

console.log(
"Assigned " +
notAssignedRoles.map(role => role.name) +
" to " +
username + " " + d
username + " ["+member.id+"] on " + new Date().toString()
);
} else {

console.log(username + " already has all the roles assigned");
}
if (Roles[0]==null && Roles[1]==null && Roles[2]==null && Roles[3]==null) {
member.removeRoles(Roles2);
console.log(username + " ["+member.id+"] has all roles removed on"+ new Date().toString());
}else{

console.log(username + " ["+member.id+"] already has all the roles assigned, checked on "+ new Date().toString());
}
}
}
});
} catch (err) {
Expand Down
Loading