首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运行不和谐命令时生成URL

运行不和谐命令时生成URL
EN

Stack Overflow用户
提问于 2022-12-04 20:02:11
回答 1查看 10关注 0票数 0

我只是在想,当用户运行一个不和谐的bot命令时,我将如何为他们生成一个URL。因此,一旦他们运行命令,它将为他们生成一个URL,然后一旦他们点击我的网站上的一个按钮,他们将被赋予一个角色。

我知道我将不得不使用discord.js & express,但我将如何做到这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 20:07:38

当用户运行不和谐的bot命令时,为他们生成一个URL,您首先需要为您的不和谐服务器创建一个bot。您可以通过访问不和谐的开发人员门户并按照那里的说明来实现这一点。

一旦创建了bot,就需要使用Discord.js库来访问不和谐的API,并使用bot执行各种操作,例如发送消息和响应用户输入。

要生成URL,可以使用discord.js库为每个用户创建唯一的代码,然后将该代码附加到基本URL中。例如:

代码语言:javascript
运行
复制
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', message => {
  if (message.content === '!generate-url') {
    // Generate a unique code for the user
    const code = generateCode(message.author.id);
    // Append the code to the base URL
    const url = `https://my-website.com/verify?code=${code}`;
    // Send the URL to the user
    message.channel.send(`Here is your URL: ${url}`);
  }
});

function generateCode(userId) {
  // Generate a unique code based on the user's ID
  return userId + '-' + Date.now();
}

一旦用户单击该URL,您就可以使用速成库创建一个服务器,该服务器侦听对该URL的请求,然后执行适当的操作,例如在您的不和谐服务器上赋予用户一个角色。

下面是一个示例,说明如何使用express创建一个服务器,该服务器侦听对/verify端点的请求并为用户提供一个角色:

代码语言:javascript
运行
复制
const Discord = require('discord.js');
const express = require('express');

const app = express();
const client = new Discord.Client();

// Listen for requests to the /verify endpoint
app.get('/verify', (req, res) => {
  // Get the code from the query string
  const code = req.query.code;

  // Look up the user associated with the code
  const user = lookupUserByCode(code);

  // Give the user the "Verified" role
  user.addRole('Verified')
    .then(() => {
      // Send a success message to the user
      res.send('You have been verified. Welcome to the server!');
    })
    .catch(err => {
      // Handle any errors that may occur
      res.send('An error occurred while verifying your account.');
    });
});

function lookupUserByCode(code) {
  // Look up the user associated with the code
  // (implementation details omitted for brevity)
}

client.login('your-bot-token-here');
app.listen(3000);

这显然只是一个例子,但我希望它作为如何处理这项任务的一般性指导有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74680429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档