Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

javascript - Discord.js with mysql (query and if not send message to channel )

i just started programming one bot for my discord, i'm facing an issue with him, i will try be specific as possible.

so i have mysql database which contains my game data, and i want to use that table so my players are able to authenticate by them self's and unlock the panel which is locked.

the table is users, and i created column discordId, and what i want to make, is when the player run the command it needs to execute SELECT * FROM users WHERE discordId = '${target.id}' , id there is not record in discordId , then try UPDATE users SET discordId = '${message.author.id}' WHERE name = '${message.author.username}', but probably the player doesn't have the same name in discord as on the game, so i want throw him a message to change his game name to the same name as discord.

i will show you my .js file

const { MessageEmbed } = require("discord.js")
const { redlight } = require("../../colours.json");

module.exports = {
config: {
    name: "auth",
    description: "Authentication to the Billing with discord!",
    usage: "%auth",
    category: "panel",
    accessableby: "Members",
    aliases: ["authentication", "access"]
},
run: async (bot, message, args, con) => {
    
    let target = message.author;

    con.query(`SELECT * FROM users WHERE discordId = '${target.id}'`, (err, req) => {
        if(err) throw err;

        
        if(!req[0].discordId) return message.channel.send(`**${message.author.username}** Please change your lord name in game to **${message.author.username}** and then do %auth`)
        message.delete();
        if(err) throw err;
        
            con.query(`UPDATE users SET discordId = '${message.author.id}' WHERE name = '${message.author.username}'`);
            
            message.channel.send(`You have been authenticated **${message.author.username}**...`);

        
        if(message.mentions.users.first()) return message.channel.send("You're not allowed to do that!");
        
        // return;

        let embed = new MessageEmbed()
            .setColor(redlight)
            .setAuthor(`${message.guild.name} Modlogs`, message.guild.iconURL)
            .addField("Panel:", "Authentication")
            .addField("Player:", message.author.username)
            .addField("Description:", "Have been authenticated")
            .addField("Date:", message.createdAt.toLocaleString())

            let sChannel = message.guild.channels.cache.find(c => c.id === "801181624809160735")
            sChannel.send(embed);
    });
}

}

i really need help here please, don't forget i'm just starting kearning how to do discord bot's.

TIA.

question from:https://stackoverflow.com/questions/65830884/discord-js-with-mysql-query-and-if-not-send-message-to-channel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found what is causing your problem from your error above.

I do not know what this line is meant to do, as you seem to be treating an array as an object, which would not work.

if(!req[0].discordId) return message.channel.send(`**${message.author.username}** Please change your lord name in game to **${message.author.username}** and then do %auth`)
message.delete();

I also cannot tell you what to replace this with since I do not know what the req array is, only that it is the thing causing your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...