我只是在想,当用户运行一个不和谐的bot命令时,我将如何为他们生成一个URL。因此,一旦他们运行命令,它将为他们生成一个URL,然后一旦他们点击我的网站上的一个按钮,他们将被赋予一个角色。
我知道我将不得不使用discord.js & express,但我将如何做到这一点。
发布于 2022-12-04 20:07:38
当用户运行不和谐的bot命令时,为他们生成一个URL,您首先需要为您的不和谐服务器创建一个bot。您可以通过访问不和谐的开发人员门户并按照那里的说明来实现这一点。
一旦创建了bot,就需要使用Discord.js库来访问不和谐的API,并使用bot执行各种操作,例如发送消息和响应用户输入。
要生成URL,可以使用discord.js
库为每个用户创建唯一的代码,然后将该代码附加到基本URL中。例如:
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
端点的请求并为用户提供一个角色:
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);
这显然只是一个例子,但我希望它作为如何处理这项任务的一般性指导有所帮助。
https://stackoverflow.com/questions/74680429
复制相似问题