当前位置:网站首页>Discord机器人开发
Discord机器人开发
2022-06-13 11:11:00 【轻松的小希】
目录
第一章 Discord账号注册
温馨提示:在阅读本文章内容之前,请确保您能正常访问外网。
官方网址: https://discord.com/
注册页面: https://discord.com/register



第二章 Discord创建服务器






第三章 Discord创建机器人
3.1、创建应用
开发者平台: https://discord.com/developers/applications


3.2、创建机器人


3.3、配置机器人

3.4、添加机器人






第四章 Discord机器人开发
温馨提示:在进行开发前,请确保您的开发环境是:jdk1.8、maven3.6.3、idea2021.1
4.1、推荐资料
4.2、创建工程


4.3、添加依赖
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-alpha.12</version>
</dependency>
</dependencies>
4.4、获取Token


4.5、编写主类
package cn.caochenlei;
import cn.caochenlei.event.MessageEvent;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
public class JavaBotApp {
private static final String TOKEN = "******替换成你自己的******";
public static void main(String[] args) throws Exception {
JDA jda = JDABuilder.createDefault(TOKEN).build().awaitReady();
jda.addEventListener(new MessageEvent());
}
}


第五章 Discord机器人初级
5.1、和机器人对话
JavaBotApp
package cn.caochenlei;
import cn.caochenlei.event.MessageEvent;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
public class JavaBotApp {
private static final String TOKEN = "******替换成你自己的******";
public static void main(String[] args) throws Exception {
JDA jda = JDABuilder.createDefault(TOKEN).build().awaitReady();
jda.addEventListener(new MessageEvent());
}
}
MessageEvent
package cn.caochenlei.event;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
import java.util.List;
public class MessageEvent extends ListenerAdapter {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
//如果是机器人发消息触发这个事件则忽略
//如果不加,则这个事件会变成死循环事件
if (event.getAuthor().isBot()) {
return;
}
Message message = event.getMessage();
//获取服务器(公会)信息
Guild guild = message.getGuild();
String guildId = guild.getId();
String guildName = guild.getName();
System.out.println("guildId = " + guildId);//输出服务器(公会)id
System.out.println("guildName = " + guildName);//输出服务器(公会)名称
//获取频道信息
MessageChannel channel = message.getChannel();
String channelId = channel.getId();
String channelName = channel.getName();
ChannelType channelType = channel.getType();
System.out.println("channelId = " + channelId);//输出频道id
System.out.println("channelName = " + channelName);//输出频道名称
System.out.println("channelType = " + channelType);//输出频道类型
//获取发消息人
Member member = message.getMember();
String memberId = member.getId();
String memberNickname = member.getNickname();
String memberEffectiveName = member.getEffectiveName();
List<Role> memberRoles = member.getRoles();
EnumSet<Permission> memberPermissions = member.getPermissions();
OnlineStatus memberOnlineStatus = member.getOnlineStatus();
System.out.println("memberId = " + memberId);//输出发送消息人的id
System.out.println("memberNickname = " + memberNickname);//输出发送消息人的昵称
System.out.println("memberEffectiveName = " + memberEffectiveName);//输出发送消息人的有效名称
System.out.println("memberRoles = " + memberRoles);//输出发送消息人的角色列表
System.out.println("memberPermissions = " + memberPermissions);//输出发送消息人的权限列表
System.out.println("memberOnlineStatus = " + memberOnlineStatus);//输出发送消息人的在线状态
//获取消息信息
String messageId = message.getId();
String messageContentRaw = message.getContentRaw();
MessageType messageType = message.getType();
System.out.println("messageId = " + messageId);//输出消息id
System.out.println("messageContentRaw = " + messageContentRaw);//输出消息内容
System.out.println("messageType = " + messageType);//输出消息类型
//回应用户消息
channel.sendMessage("bot already received").queue();
}
}


持续更新中…
边栏推荐
- Go zero microservice Practice Series (III. API definition and table structure design)
- Private computing fat core concepts and stand-alone deployment
- View the default MySQL password in the pagoda
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
- [tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
- 状态压缩DP例题(旅行商问题和填矩形问题)
- Database learning notes (Chapter 16)
- 避免让转型企业走入歧途,是时候重新理解下湖仓一体了!| Q推荐
- 数位DP例题
- Do you agree that the salary of hardware engineers is falsely high?
猜你喜欢

ue5 小知识点 random point in Bounding Boxf From Stream

塔米狗知识|全面剖析国有企业并购含义及其作用

Chapter VII document management

Database system concept (Chapter 17)

很妙的贪心(F2. Nearest Beautiful Number (hard version))

硬件工程师薪资虚高,你认可吗?

Folder data synchronization tool sync folders Pro

2021CCPC网络赛题解加总结

Socket programming (medium)
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
随机推荐
MFC自定义button实现颜色控制
【TcaplusDB知识库】TcaplusDB机型管理介绍
Necessary for Architects: system capacity status checklist
Similarities and differences between commonAPI and AUTOSAR AP communication management
Miidock file distribution
Pagoda add a website: PHP project
Ue5 random point in bounding boxf from stream
C file package and download
Will it be a great opportunity for entrepreneurs for Tiktok to attach so much importance to live broadcast sales of takeout packages?
【20220526】UE5.0.2 release d11782b
Database learning notes (Chapter 15)
【TcaplusDB知识库】TcaplusDB新增机型介绍
【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍
Ubuntu安装mysql压缩包备查
Web 3.0?高成本版的P2P而已
DNS protocol analysis
Multithreading starts from the lockless queue of UE4 (thread safe)
Tamidog knowledge | a comprehensive analysis of the meaning and role of mergers and acquisitions of state-owned enterprises
Go needs to add an arrow syntax, which is more like PHP!
Show/exec and close/hide of QT form are not executed when calling the close destructor