【问题标题】:How to make it give every ticket a unique number?, Discord.js如何让它给每张票一个唯一的号码?,Discord.js
【发布时间】:2021-12-10 05:56:56
【问题描述】:

它是如何工作的:

我正在努力做到这一点,如果有人对工作人员发送的消息做出反应,它会打开一张票。

问题:

我想使用成员的 id,但它太大了。 我尝试使用播放器的名称,但由于某种原因它没有找到它作为频道,也许唯一的数字更好?

我使用的是命令处理系统,所以你不会看到任何基本的东西(例如client.login

这是我的代码:

const { Discord } = require('discord.js');
const axios = require("axios")

module.exports = {
    name: 'thing',
    category: 'Owner',
    aliases: ["t"],
    description: 'thing command.',
    usage: 'thing',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
message.channel.send('Click "⚔" to open a ticket!').then(function(message) {
message.react('⚔');
const filter = (reaction, user) => {
    return ['⚔'].includes(reaction.emoji.name) && user.id === message.author.id;
}});

client.on('messageReactionAdd', (reaction, ruser) => {
if (!ruser.bot) {
if (reaction.emoji.name == '⚔') {
        if(message.guild.channels.cache.find(channel => channel.name == (`t-${ruser.id}`))) {
            return message.channel.send('<@' + ruser.id + '> you already have a ticket, please close your existing ticket first before opening a new one!')
      .then(m => m.delete({timeout: 3000}));
        }

        message.guild.channels.create(`t-${ruser.id}`, {
            permissionOverwrites: [
                {
                    id: message.author.id,
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: message.guild.roles.everyone,
                    deny: ['VIEW_CHANNEL'],
                },
            ],
            type: 'text',
        }).then(async channel => {
            message.channel.send(`<@` + ruser.id + `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`)
      .then(m => m.delete({timeout: 3000}));
            channel.send(`Hi <@` + ruser.id + `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
            const logchannel = message.guild.channels.cache.find(channel => channel.name === 'server-logs');
            if(logchannel) {
                logchannel.send(`Ticket-${ruser.username} created. Click the following to veiw <#${channel.id}>`);
            }
        });
    }
  }});

}}

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    有多种方法可以做到这一点。我将创建一个&lt;Map&gt; 对象,其键应代表用户 ID,并为属于该用户的票证 ID 赋值。我还将声明一个变量ticketID,每次创建新票证时该变量都会增加 1。在每个反应中,机器人应该检查用户 ID 是否是地图的现有键,如果不是,则应该在其中创建一个新条目:

    (...)
    let ticketCounter = 0;
    const userTickets = new Map();
    
    client.on('messageReactionAdd', (reaction, ruser) => {
        if(!ruser.bot) {
            if(reaction.emoji.name == '⚔') {
                if(userTickets.has(ruser.id)) {
                    return message.channel.send('<@' + ruser.id + '> you already have a ticket, please close your existing ticket first before opening a new one!').then(m => m.delete({timeout: 3000}));
                }
                
                message.guild.channels.create(`t-${ticketCounter}`, {
                    permissionOverwrites: [
                        {
                            id: message.author.id,
                            allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                        },
                        {
                            id: message.guild.roles.everyone,
                            deny: ['VIEW_CHANNEL'],
                        },
                    ],
                    type: 'text',
                }).then(async channel => {
                    userTickets.set(ruser.id, ticketCounter++); // This will create the map entry whose value is the previous ticketCounter value, the ++ increments afterwards.
                    
                    message.channel.send(`<@` + ruser.id + `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`).then(m => m.delete({timeout: 3000}));
                    channel.send(`Hi <@` + ruser.id + `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
                    const logchannel = message.guild.channels.cache.find(channel => channel.name === 'server-logs');
                    
                    if(logchannel) {
                        logchannel.send(`Ticket-${ruser.username} created. Click the following to veiw <#${channel.id}>`);
                    }
                });
            }
        }
    });
    

    【讨论】:

    • 谢谢,这很有帮助!
    猜你喜欢
    • 2017-12-03
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多