Discord bot

JoshuaS

Learning
Jan 9, 2014
119
13
Hi devbest.

I' was wondering if any over you had insight in a bot that that has the purpose of getting the usernames of users from a database table, and then set the nickname on the person on discord using a verify link. Like devbest themselves are doing.

Atm i have no idea where to look, if you have any insight or some tricks in creating the bot yourself please let me know.

Sorry if this is the wrong channel to write it in. Couldn't find any that worked better for the purpose of the thread.
 

Khalil

IDK
Dec 6, 2011
1,642
786
This can easily be achieved using OAuth and a bot with access to your database.

The process would be as such:
- Have the user authenticate through Discord's OAuth service while their session is active on your website.
- Grab their Discord ID following the authentication process and add it to their user information.
- Grant the bot access to your database, the point at which it would look for a Discord ID upon a new server join, match it to an existing user on your server and change their nickname to correspond with their username.

The implementation would roughly look something like this:
CoffeeScript:
MySQL = require "mysql"
Discord = require "discord.js"

# This database has to be the same one you're using for your website.
Connection = MySQL.createConnection { host: "localhost", user: "something", password: "something", database: "database" }
Client = new Discord.Client

Client.on "ready", () ->
    console.log "Client logged on as " + Client.user.username + "#" + Client.user.discriminator

# The bot would listen for new users.
Client.on "guildMemberAdd", (Member) ->
    # We check to see if the new user's ID exists in our records.
    Connection.query "SELECT * FROM users WHERE discord_id = ?", [Member.id], (Error, Rows) ->
        if (Error)
            throw Error

        # Found them.
        if Rows.length
            # Check if the bot has permission to change other users' nicknames.
            if Member.guild.me.hasPermission ["MANAGE_NICKNAMES"]
                # Change their nickname to correspond with their existing username on your database.
                Member.setNickname Rows[0].username

    Connection.end
    
Client.login "YOUR BOT TOKEN"
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
This can easily be achieved using OAuth and a bot with access to your database.

The process would be as such:
- Have the user authenticate through Discord's OAuth service while their session is active on your website.
- Grab their Discord ID following the authentication process and add it to their user information.
- Grant the bot access to your database, the point at which it would look for a Discord ID upon a new server join, match it to an existing user on your server and change their nickname to correspond with their username.

The implementation would roughly look something like this:
CoffeeScript:
MySQL = require "mysql"
Discord = require "discord.js"

# This database has to be the same one you're using for your website.
Connection = MySQL.createConnection { host: "localhost", user: "something", password: "something", database: "database" }
Client = new Discord.Client

Client.on "ready", () ->
    console.log "Client logged on as " + Client.user.username + "#" + Client.user.discriminator

# The bot would listen for new users.
Client.on "guildMemberAdd", (Member) ->
    # We check to see if the new user's ID exists in our records.
    Connection.query "SELECT * FROM users WHERE discord_id = ?", [Member.id], (Error, Rows) ->
        if (Error)
            throw Error

        # Found them.
        if Rows.length
            # Check if the bot has permission to change other users' nicknames.
            if Member.guild.me.hasPermission ["MANAGE_NICKNAMES"]
                # Change their nickname to correspond with their existing username on your database.
                Member.setNickname Rows[0].username

    Connection.end
   
Client.login "YOUR BOT TOKEN"

Idc if this is off-topic, I'm extremely impressed with how much you've learned, just from messing with the Discord API
 

Users who are viewing this thread

Top