Discord Bot for creating accounts, rules acceptance.

Got any custom JavaScript additions/tweaks you think other people would like to see? Post 'em here!
Post Reply
Azzerhoden
UOX3 Newbie
Posts: 16
Joined: Wed Oct 05, 2022 3:10 pm
Has thanked: 0
Been thanked: 8 times

Discord Bot for creating accounts, rules acceptance.

Post by Azzerhoden »

On my discord server for the Hyperion UOX3 server, I have a discord bot up and running that, along with permissions, requires that players accept the server rules, then after accepting those rules, allows them to create their own accounts. This script was built for my own discord server, but you should be able to use it as a starting point.

First, I have 3 channels on the server that serve as the starting point.
Welcome - the channel for invite links
Server Rules - Where I post the rules for the server.
Account Creation - Where players create accounts.

When a player first joins the discord server, they only see the first 2 channels. After they accept the rules, they see the 3rd. I do this by limiting access to the first two to the @everyone role, and the third channel to a new role named 'New Members'. As players step through the process their role gets updated, until they get the role 'Players'. On a side note, I setup my discord channels to use one set of roles for access, the other for channel appearance. For example, 'New Members' and 'Players' are for appearance. The actual access is granted by the role [player access]. This allows me to easily control who gets access to what, regardless of how they appear on the server. I grant permissions starting at the lowest level, then I move up, leaving all previous roles enabled.

You also have to go into settings, and enable the Developer switch. This will allow you to right click on a channel and see the channel ID, which is used in the discord bot script, which is written in Javascript. Here is the server code. If you have any ideas on how to improve this script, please share them with me. :)

const Discord = require('discord.js');
const { Client, Events, GatewayIntentBits } = require ('discord.js');
const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent,],
});
const fs = require('fs');
const currentDate = new Date();

function genPassword (count=10) {
    let password = '';
    while (password.length < count) {
      password += Math.random().toString(36).substring(2);
    }
    return password.substring(0, count);
}

client.once(Events.ClientReady, () => { console.log('Registration bot is online.'); });


client.on("messageCreate", message => {
    let sender = message.author.username;
    let senderID = message.author.id;
    let roleNewMembers = message.guild.roles.cache.find( r => r.name == 'New Members');
    let rolePlayers = message.guild.roles.cache.find( r => r.name == 'Players');
    let playerAccess = message.guild.roles.cache.find( r => r.name == '[player access]');
    if (message.channelId == /* CHANNEL ID for SERVER RULES */)
    {
        if (message.content.toString() === '!Accept' || message.content.toString() === '!accept')
        {
            message.author.send('You have accepted the rules for the Hyperion UOX3 server.');
            message.delete();
            let accept = 'Server Rules Accepted by (Name):' + sender + ' (ID):' + senderID + ' (Date): ' + currentDate + '\n';
            console.log(accept);
            fs.appendFile('UserAcceptanceLog.txt', accept, err => {
                if (err) {
                    console.error(err);
                    return;
                }
            });
            message.guild.members.cache.get(senderID).roles.add(roleNewMembers);
            message.guild.members.cache.get(senderID).roles.add(playerAccess);
        }
        else
        {
            message.author.send('You entered a message that is not understood and has been removed. You must accept these rules with !Accept to proceed.');
            message.delete();
            let reject = 'Incorrect entry by (Name):' + sender + ' (ID):' + senderID + ' (Date): ' + currentDate + '\n';
            console.log(reject);
            fs.appendFile('UserRejectLog.txt', reject, err => {
                if (err) {
                    console.error(err);
                    return;
                }
            });
        }
    }
    else if (message.channelId == /* CHANNEL ID for CREATE ACCOUNTS */)
    {
        if (message.content.startsWith('!Create') || message.content.startsWith('!create'))
        {
            const acct = message.content.split(' ').slice(1).join(' ');
            const pwd = genPassword(10);
            const perms= 0x0000;
            const info = sender + '::' + senderID;
            if (acct.length > 5 && acct.length < 16 && (/^[a-zA-Z0-9 ]*$/.test(acct)))
            {
                message.author.send('Welcome to the Hyperion UOX3 server! \n ' +
                                'As a reminder - it can take up to 10 minutes for your account to be registered on the game server. \n' +
                                'Please use the image provided in the account-creation channel as a guide. \n' +
                                'Your account has been created using the account name: ' + acct + ' with the password: ' + pwd);
                message.delete();
                fs.appendFile('/* GAME SERVER PATH */\\newaccounts.adm', 'USER=' + acct + ',' + pwd + ',' + perms + ',' + info + '\n', err => {
                    if (err) {
                        console.error(err);
                        return;
                    }
                });
                message.guild.members.cache.get(senderID).roles.add(rolePlayers);
            }
            else
            {
                message.author.send('Account names must be between 6 and 12 characters in length and contain only letters or numbers.  Please try again.');
                message.delete();
            }
        }
        else
        {
            message.author.send('You entered a message that is not understood and has been removed. Only !Create <account name> will be recognized.');
            message.delete();
        }
}

});

client.login(/* YOUR TOKEN GOES HERE */);
FINALLY - keep your server token PRIVATE at all times!
These users thanked the author Azzerhoden for the post:
Xuri
Post Reply