diff --git a/Image/highrole.JPG b/Image/highrole.JPG new file mode 100644 index 0000000..cf2afa2 Binary files /dev/null and b/Image/highrole.JPG differ diff --git a/Image/permission.jpg b/Image/permission.jpg new file mode 100644 index 0000000..78468e6 Binary files /dev/null and b/Image/permission.jpg differ diff --git a/README.md b/README.md index fff0b10..c63b62c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,125 @@ -# form2role-bot -Discord bot that automatically assigns roles to users found in a Google Sheet +Form2Role-Bot: +

About

+

This is the simplified version of talios0's and The24thDS's 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.

+

Motivation

+

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.

+ +

Required fields

+

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)

+

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).

+ +

How to get your Google details

+

API key

+ +

Spreadsheet ID

+ +

Range

+ +

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)

+

How to setup a Discord bot

+

You will need a bot token, to generate one follow these steps:

+ + +

Invite the bot to your discord server:

+ +

Configuration steps

+ +

More info

+ + + +

New updates on 28/06/2020

+ +
  • 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
  • +
  • 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"]
  • +
  • The bot was doing nothing when all roles were removed, it detects and removes all roles now
  • +
  • Now it throws time in console when the bot assign or remove roles of a user
  • + + + +

    New Information updates on 22/06/2022

    +
    +

    Always add "@everyone" and other unremovable roles in unremovable roles under config/discord.json

    +
    +

    You should manually give more permission to your bot roles as shown in the picture below

    +
  • Right click on the server "icon"
  • +
  • Click on "Server Setting" from drop menu"
  • +
  • Click "Roles" from the drop menu of "server settings
  • +
  • Click "Roles" menu click on the "role" assigned to your bot
  • +
  • Click "Roles" menu click on the "role" assigned to your bot
  • +
  • Go to "Permissions" Tab and give all permissions to your bot
  • + +![Image of Permission](https://github.com/supreen/form2role-bot/blob/master/Image/permission.jpg?raw=true) +
    +

    Your bot role much be higher than another role as shown in the picture below

    +
  • Right click on the server "icon"
  • +
  • Click on "Server Setting" from drop menu"
  • +
  • Click "Roles" from the drop menu of "server settings
  • +
  • drag the bot role above all roles
  • +![Image of higher role](https://github.com/supreen/form2role-bot/blob/master/Image/highrole.JPG?raw=true) +
    + + +

    New demo added on 22/06/2022

    +
  • Spreadsheet link:https://docs.google.com/spreadsheets/d/1p1GiIqUUy2b-HcsaCJFHu_ZYQ2nWKZ2zGXmEmu9RrNE/edit#gid=0
  • +
  • Discord chaneel: https://discord.com/channels/715245634218885131/715245634898100256
  • + +

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

    + + + +

    Consider buying me a coffee, if you loved my work.

    +
  • https://www.buymeacoffee.com/suprin + +

    If you are still finding it hard, Hire me

    +
  • https://sobdar.com/order (you get 30% off here and you can also put your query there) +
  • https://www.fiverr.com/supreen + +

    For more contact please visit:

    +

  • https://sobdar.com/ (Official Website) +
  • https://sobdar.com/wa (Whatsapp) +
  • https://sobdar.com/fbmsg (Messenger) +
  • https://sobdar.com/fb (fbpage)
  • diff --git a/config/discord.json b/config/discord.json new file mode 100644 index 0000000..33b4fdd --- /dev/null +++ b/config/discord.json @@ -0,0 +1,13 @@ +{ + "token": "", +"refreshrate":"60000", + "range":"A2:E", + "spreadsheetId":"", +"apiKey":"", + "unremoveableroles":[""], + "usernameorid":"", + "refreshrate":"60000", + "roles": [], + "usernamesp": [] + +} diff --git a/index.js b/index.js index a8a90ec..37ad577 100644 --- a/index.js +++ b/index.js @@ -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({ @@ -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()); @@ -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 @@ -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 = []; @@ -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); } @@ -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) { diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..12495bf --- /dev/null +++ b/public/index.html @@ -0,0 +1,85 @@ + + + + +
    + + +
    + + + + + + + + + + + + + + + + + + + + + + +