<commit_before>const Command = require("../base/Command");

class Poll extends Command {
    constructor(client) {
        super(client, {
            name: "poll",
            category: "Misc",
            aliases: ["vote"],
            flags: {
                anon: {
                    aliases: ["anonymous"]
                }
            },
            subArgs: {
                pollID: {
                    aliases: ["poll"],
                    default: null
                }
            }
        });
    }

    async run(client, message, [action, ...opts], options) {
        const level = options.level;
        if (!action) {
            return message.channel.send(message.language.get("COMMAND_POLL_NO_ARG"));
        }
        let poll = { // ID = guildID-channelID
            "question": "",
            "options": [],
            "votes": {
                // userID: vote#
            },
            "anon": false
        };
        const optsJoin = opts.join(" ").split("|");
        let pollID;
        let exists = false;
        // If subArgs.poll is there, get that instead, and check if it's for a channel 
        // that the user has access to
        if (options.subArgs.pollID) {
            // If they're using the auto-incrementing ID to vote from anywhere
            exists = await client.database.models.polls.findOne({where: {pollId: options.subArgs.pollID}})
                .then(token => token != null)
                .then(isUnique => isUnique);

            if (exists) {
                const tempP = await client.database.models.polls.findOne({where: {pollId: options.subArgs.pollID}, attributes: ["id", "poll", "pollId"]});
                const thisPoll = tempP.dataValues;
                poll = thisPoll.poll;
                poll.pollID = thisPoll.pollId;
                pollID = thisPoll.id;
                const [guildID, chanID] = pollID.split("-");

                // If they do not have access to the channel, tell em so/ don't let them interact with the poll (see/ vote)
                if (client.guilds.has(guildID) && client.guilds.get(guildID).channels.has(chanID)) {
                    const perms = client.guilds.get(guildID).channels.get(chanID).permissionsFor(message.author.id);
                    if (!perms || !perms.has("READ_MESSAGES")) {
                        // The guild and channel exist, but the message author cannot see it
                        return message.channel.send(message.language.get("COMMAND_POLL_NO_ACCESS"));
                    }
                }
                if (message.guild && message.guild.id !== guildID) {
                    // Make it so remote voting is only usable within DMs or in the same guild
                    return message.channel.send("Sorry, but you must be in a DM or in the same server to vote remotely.");
                }
            } else {
                return message.channel.send(message.language.get("COMMAND_POLL_INVALID_ID"));
            }
            const actions = ["view", "check"];
            if (!actions.includes(action.toLowerCase()) && !action.match(/\d/)) {
                return message.channel.send(message.language.get("COMMAND_POLL_REMOTE_OPTS"));
            }
            // If they get past all the stuff before here, they should be good to go
        } else if (!message.guild) {
            // If they're trying to use this in a DM
            return message.channel.send(message.language.get("COMMAND_POLL_DM_USE", message.guildSettings.prefix));
        } else {
            // If they're just voting on the channel's poll
            pollID = `${message.guild.id}-${message.channel.id}`;
            exists = await client.database.models.polls.findOne({where: {id: pollID}})
                .then(token => token != null)
                .then(isUnique => isUnique);

            if (exists) {
                const tempP = await client.database.models.polls.findOne({where: {id: pollID}});
                poll = tempP.dataValues.poll;
                poll.pollID = tempP.dataValues.pollId;
            }
        }
        switch (action) {
            case "start":
            case "create": {
                // Create a poll (lvl 3+)
                if (level < 3) {
                    return message.channel.send(message.language.get("COMMAND_MISSING_PERMS"));
                }
                if (exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_ALREADY_RUNNING"));
                }
                if (!optsJoin[0]) {
                    return message.channel.send(message.language.get("COMMAND_POLL_MISSING_QUESTION"));
                } else {
                    poll.question = optsJoin[0];
                    optsJoin.splice(0,1);
                    if (poll.question.length >= 256) {
                        return message.channel.send(message.language.get("COMMAND_POLL_TITLE_TOO_LONG"));
                    }
                }
                if (optsJoin.length < 2) {
                    return message.channel.send(message.language.get("COMMAND_POLL_TOO_FEW_OPT"));
                } else if (optsJoin.length > 10) {
                    return message.channel.send(message.language.get("COMMAND_POLL_TOO_MANY_OPT"));
                } else {
                    poll.options = optsJoin.map(opt => opt.trim());
                }
                if (options.flags.anon) {
                    poll.anon = true;
                }
                await client.database.models.polls.create({
                    id: pollID,
                    poll: poll
                })
                    .then((thisPoll) => {
                        poll.pollID = thisPoll.dataValues.pollId;
                        return message.channel.send(message.language.get("COMMAND_POLL_CREATED", message.author.tag, message.guildSettings.prefix), {embed: {
                            author: {
                                name: poll.question
                            },
                            description: pollCheck(poll),
                            footer: getFooter(poll)
                        }});
                    });               
                break;
            } 
            case "view":
            case "check": {
                // Check the current poll
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    if (poll.question.length > 256) {
                        // Should not happen, but just in case
                        return message.channel.send({embed: {
                            author: {
                                name: "<NAME>"
                            },
                            description: `**${poll.question}**\n\n${pollCheck(poll)}`,
                            footer: getFooter(poll)
                        }});
                    } else {
                        return message.channel.send({embed: {
                            author: {
                                name: poll.question
                            },
                            description: pollCheck(poll),
                            footer: getFooter(poll)
                        }});
                    }
                }
            }
            case "close":
            case "end": {
                // Close the current poll and send the results (lvl 3+)
                if (level < 3) {
                    return message.channel.send(message.language.get("COMMAND_MISSING_PERMS"));
                }
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    // Delete the current poll
                    await client.database.models.polls.destroy({where: {id: pollID}})
                        .then(() => {
                            // Then send a message showing the final results
                            return message.channel.send({embed: {
                                author: {
                                    name: message.language.get("COMMAND_POLL_FINAL", "")
                                },
                                description: `**${poll.question}**\n${pollCheck(poll, true)}`
                            }});
                        })
                        .catch(() => { 
                            // Or, if it breaks, tell them that it broke
                            return message.channel.send(message.language.get("COMMAND_POLL_FINAL_ERROR", poll.question));
                        });
                }
                break;
            }
            case String(action.match(/\d/)): {
                // Someone voting on an option
                // Check if there is a poll going, then if there is, vote, else tell em that there isn"t 
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    const opt = Math.abs(parseInt(action)-1);
                    if (poll.options.length < opt) {
                        return message.channel.send(message.language.get("COMMAND_POLL_INVALID_OPTION"));
                    } else {
                        let voted = -1;
                        if (poll.votes[message.author.id] === opt) {
                            return message.channel.send(message.language.get("COMMAND_POLL_SAME_OPT", poll.options[opt]));
                        } else if (poll.votes.hasOwnProperty(message.author.id)) {
                            voted = poll.votes[message.author.id];
                        }
                        poll.votes[message.author.id] = opt;
                        await client.database.models.polls.update({poll: poll}, {where: {id: pollID}})
                            .then(() => {
                                if (voted !== -1) {
                                    return message.channel.send(message.language.get("COMMAND_POLL_CHANGED_OPT", poll.options[voted], poll.options[opt]));
                                } else {
                                    return message.channel.send(message.language.get("COMMAND_POLL_REGISTERED", poll.options[opt]));<commit_msg>Fix for bad option checking<commit_after>const Command = require("../base/Command");

class Poll extends Command {
    constructor(client) {
        super(client, {
            name: "poll",
            category: "Misc",
            aliases: ["vote"],
            flags: {
                anon: {
                    aliases: ["anonymous"]
                }
            },
            subArgs: {
                pollID: {
                    aliases: ["poll"],
                    default: null
                }
            }
        });
    }

    async run(client, message, [action, ...opts], options) {
        const level = options.level;
        if (!action) {
            return message.channel.send(message.language.get("COMMAND_POLL_NO_ARG"));
        }
        let poll = { // ID = guildID-channelID
            "question": "",
            "options": [],
            "votes": {
                // userID: vote#
            },
            "anon": false
        };
        const optsJoin = opts.join(" ").split("|");
        let pollID;
        let exists = false;
        // If subArgs.poll is there, get that instead, and check if it's for a channel 
        // that the user has access to
        if (options.subArgs.pollID) {
            // If they're using the auto-incrementing ID to vote from anywhere
            exists = await client.database.models.polls.findOne({where: {pollId: options.subArgs.pollID}})
                .then(token => token != null)
                .then(isUnique => isUnique);

            if (exists) {
                const tempP = await client.database.models.polls.findOne({where: {pollId: options.subArgs.pollID}, attributes: ["id", "poll", "pollId"]});
                const thisPoll = tempP.dataValues;
                poll = thisPoll.poll;
                poll.pollID = thisPoll.pollId;
                pollID = thisPoll.id;
                const [guildID, chanID] = pollID.split("-");

                // If they do not have access to the channel, tell em so/ don't let them interact with the poll (see/ vote)
                if (client.guilds.has(guildID) && client.guilds.get(guildID).channels.has(chanID)) {
                    const perms = client.guilds.get(guildID).channels.get(chanID).permissionsFor(message.author.id);
                    if (!perms || !perms.has("READ_MESSAGES")) {
                        // The guild and channel exist, but the message author cannot see it
                        return message.channel.send(message.language.get("COMMAND_POLL_NO_ACCESS"));
                    }
                }
                if (message.guild && message.guild.id !== guildID) {
                    // Make it so remote voting is only usable within DMs or in the same guild
                    return message.channel.send("Sorry, but you must be in a DM or in the same server to vote remotely.");
                }
            } else {
                return message.channel.send(message.language.get("COMMAND_POLL_INVALID_ID"));
            }
            const actions = ["view", "check"];
            if (!actions.includes(action.toLowerCase()) && !action.match(/\d/)) {
                return message.channel.send(message.language.get("COMMAND_POLL_REMOTE_OPTS"));
            }
            // If they get past all the stuff before here, they should be good to go
        } else if (!message.guild) {
            // If they're trying to use this in a DM
            return message.channel.send(message.language.get("COMMAND_POLL_DM_USE", message.guildSettings.prefix));
        } else {
            // If they're just voting on the channel's poll
            pollID = `${message.guild.id}-${message.channel.id}`;
            exists = await client.database.models.polls.findOne({where: {id: pollID}})
                .then(token => token != null)
                .then(isUnique => isUnique);

            if (exists) {
                const tempP = await client.database.models.polls.findOne({where: {id: pollID}});
                poll = tempP.dataValues.poll;
                poll.pollID = tempP.dataValues.pollId;
            }
        }
        switch (action) {
            case "start":
            case "create": {
                // Create a poll (lvl 3+)
                if (level < 3) {
                    return message.channel.send(message.language.get("COMMAND_MISSING_PERMS"));
                }
                if (exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_ALREADY_RUNNING"));
                }
                if (!optsJoin[0]) {
                    return message.channel.send(message.language.get("COMMAND_POLL_MISSING_QUESTION"));
                } else {
                    poll.question = optsJoin[0];
                    optsJoin.splice(0,1);
                    if (poll.question.length >= 256) {
                        return message.channel.send(message.language.get("COMMAND_POLL_TITLE_TOO_LONG"));
                    }
                }
                if (optsJoin.length < 2) {
                    return message.channel.send(message.language.get("COMMAND_POLL_TOO_FEW_OPT"));
                } else if (optsJoin.length > 10) {
                    return message.channel.send(message.language.get("COMMAND_POLL_TOO_MANY_OPT"));
                } else {
                    poll.options = optsJoin.map(opt => opt.trim());
                }
                if (options.flags.anon) {
                    poll.anon = true;
                }
                await client.database.models.polls.create({
                    id: pollID,
                    poll: poll
                })
                    .then((thisPoll) => {
                        poll.pollID = thisPoll.dataValues.pollId;
                        return message.channel.send(message.language.get("COMMAND_POLL_CREATED", message.author.tag, message.guildSettings.prefix), {embed: {
                            author: {
                                name: poll.question
                            },
                            description: pollCheck(poll),
                            footer: getFooter(poll)
                        }});
                    });               
                break;
            } 
            case "view":
            case "check": {
                // Check the current poll
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    if (poll.question.length > 256) {
                        // Should not happen, but just in case
                        return message.channel.send({embed: {
                            author: {
                                name: "Current Poll"
                            },
                            description: `**${poll.question}**\n\n${pollCheck(poll)}`,
                            footer: getFooter(poll)
                        }});
                    } else {
                        return message.channel.send({embed: {
                            author: {
                                name: poll.question
                            },
                            description: pollCheck(poll),
                            footer: getFooter(poll)
                        }});
                    }
                }
            }
            case "close":
            case "end": {
                // Close the current poll and send the results (lvl 3+)
                if (level < 3) {
                    return message.channel.send(message.language.get("COMMAND_MISSING_PERMS"));
                }
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    // Delete the current poll
                    await client.database.models.polls.destroy({where: {id: pollID}})
                        .then(() => {
                            // Then send a message showing the final results
                            return message.channel.send({embed: {
                                author: {
                                    name: message.language.get("COMMAND_POLL_FINAL", "")
                                },
                                description: `**${poll.question}**\n${pollCheck(poll, true)}`
                            }});
                        })
                        .catch(() => { 
                            // Or, if it breaks, tell them that it broke
                            return message.channel.send(message.language.get("COMMAND_POLL_FINAL_ERROR", poll.question));
                        });
                }
                break;
            }
            case String(action.match(/\d/)): {
                // Someone voting on an option
                // Check if there is a poll going, then if there is, vote, else tell em that there isn"t 
                if (!exists) {
                    return message.channel.send(message.language.get("COMMAND_POLL_NO_POLL"));
                } else {
                    const opt = Math.abs(parseInt(action)) - 1;
                    if (poll.options.length <= opt || opt < 0) {
                        return message.channel.send(message.language.get("COMMAND_POLL_INVALID_OPTION"));
                    } else {
                        let voted = -1;
                        if (poll.votes[message.author.id] === opt) {
                            return message.channel.send(message.language.get("COMMAND_POLL_SAME_OPT", poll.options[opt]));
                        } else if (poll.votes.hasOwnProperty(message.author.id)) {
                            voted = poll.votes[message.author.id];
                        }
                        poll.votes[message.author.id] = opt;
                        await client.database.models.polls.update({poll: poll}, {where: {id: pollID}})
                            .then(() => {
                                if (voted !== -1) {
                                    return message.channel.send(message.language.get("COMMAND_POLL_CHANGED_OPT", poll.options[voted], poll.options[opt]));
                                } else {
                                    return message.channel.send(message.language.get("COMMAND_POLL_REGISTERED", poll.options[opt]));
{'fraction_non_alphanumeric': 0.10980810234541578, 'fraction_numerical': 0.0019868191509982555, 'mean_word_length': 1.1838095238095239, 'pattern_counts': {'":': 20, '<': 13, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3168365', 'n_tokens_mistral': 5257, 'n_tokens_neox': 4972, 'n_words': 1073}