This repository was archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (67 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const config = require('./config.json');
const {Client} = require('discord.js');
const client = new Client();
client.login(config.token);
client.on('ready',()=>{
console.log('Ready');
});
client.on('message',(msg)=>{
const text = msg.content;
if(text.startsWith('!register')){
registerSlashCommands(msg.guild);
msg.reply('Slash command register successful')
}
});
client.on('raw',(evt)=>{
if(evt.t !== 'INTERACTION_CREATE') return;
const {d: data} = evt;
if(data.type !== 2) return;
const CommandData = data.data;
if(CommandData.name === 'hello'){
let TargetUser;
if(CommandData.options) TargetUser = CommandData.options.find(element => element.name == 'mention');
const channel = client.channels.cache.get(data.channel_id);
if(TargetUser){
const user = client.users.cache.get(TargetUser.value);
callback(data,`${user} hello!`);
}else{
callback(data,`hello!`);
}
}
})
function registerSlashCommands(guild){
const data = {}
data.name = "hello"
data.description = "say hello"
data.options = new Array();
const option = {};
option.name = "mention"
option.description = "say hello to mention user"
/*
* type list:
* 1 = SubCommand
* 2 = SubCommandGroup
* 3 = String
* 4 = Integer
* 5 = Boolean
* 6 = User
* 7 = Channel
* 8 = Role
*/
option.type = 6 // 6 = User
option.required = false
data.options.push(option);
client.api.applications(client.user.id).guilds(guild.id).commands().post({data});
}
function callback(eventdata,message){
const data = {
"type": 4,
"data": {
"tts": false,
"content": message,
"embeds": [],
"allowed_mentions": []
}
}
client.api.interactions(eventdata.id)[eventdata.token].callback().post({data});
}