-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move telegram and discord bot config to mysql (#242)
* Add server config cache support * move bot config to MySQL
- Loading branch information
Showing
19 changed files
with
230 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/dl/officialsite/bot/config/BotServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dl.officialsite.bot.config; | ||
|
||
import com.dl.officialsite.config.bean.Configurable; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
public class BotServerConfig implements Configurable { | ||
List<BotGroup> groupList; | ||
String botToken; | ||
String timeoutInSeconds; | ||
} |
10 changes: 5 additions & 5 deletions
10
...m/dl/officialsite/bot/model/BotTopic.java → .../dl/officialsite/bot/config/BotTopic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package com.dl.officialsite.bot.model; | ||
package com.dl.officialsite.bot.config; | ||
|
||
import com.dl.officialsite.bot.constant.ChannelEnum; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class BotTopic<T> { | ||
public class BotTopic { | ||
private ChannelEnum name; | ||
private T threadOrTopicId; | ||
private String threadOrTopicId; | ||
|
||
public static <T> BotTopic<T> build(ChannelEnum channelEnum, T threadOrTopicId){ | ||
public static BotTopic build(ChannelEnum channelEnum, String threadOrTopicId) { | ||
BotTopic botTopic = new BotTopic(); | ||
botTopic.setName(channelEnum); | ||
botTopic.setThreadOrTopicId(threadOrTopicId); | ||
return botTopic; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package com.dl.officialsite.bot.constant; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum BotEnum { | ||
ALL, TELEGRAM, DISCORD; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/dl/officialsite/bot/discord/DiscordBot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.dl.officialsite.bot.discord; | ||
|
||
import com.dl.officialsite.bot.config.BotServerConfig; | ||
import com.dl.officialsite.bot.config.BotTopic; | ||
import com.dl.officialsite.bot.constant.BotEnum; | ||
import com.dl.officialsite.bot.constant.ChannelEnum; | ||
import com.dl.officialsite.bot.model.BaseBot; | ||
import com.dl.officialsite.config.constant.ConfigEnum; | ||
import com.dl.officialsite.config.service.ServerConfigCacheService; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.JDABuilder; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Data | ||
@EqualsAndHashCode(callSuper = false) | ||
@Service | ||
public class DiscordBot extends BaseBot<JDA> { | ||
@Autowired | ||
private ServerConfigCacheService serverConfigCacheService; | ||
|
||
@EventListener(ApplicationReadyEvent.class) | ||
public void startUp() { | ||
this.botEnum = BotEnum.DISCORD; | ||
this.botServerConfig = serverConfigCacheService.get(ConfigEnum.DISCORD_BOT_CONFIG, BotServerConfig.class); | ||
Optional.ofNullable(this.botServerConfig).ifPresent(config -> { | ||
this.init(); | ||
}); | ||
} | ||
|
||
@Override | ||
public void initBot() { | ||
try { | ||
bot = JDABuilder.createDefault(this.botServerConfig.getBotToken()).build().awaitReady(); | ||
log.info("Init Guild(server) and threads map..."); | ||
this.botServerConfig.getGroupList().forEach(guildConfig -> { | ||
Guild guild = bot.getGuildById(guildConfig.getGroupId()); | ||
if (guild == null) { | ||
log.error("Cannot find Guild for id:[{}]", guildConfig.getGroupId()); | ||
return; | ||
} | ||
List<TextChannel> textChannels = guild.getTextChannels(); | ||
if (CollectionUtils.isEmpty(textChannels)) { | ||
log.error("Cannot find any text channel for id:[{}]", guildConfig.getGroupId()); | ||
return; | ||
} | ||
List<BotTopic> botTopicList = textChannels.stream() | ||
.map(textChannel -> BotTopic.build(ChannelEnum.of(textChannel.getName()), textChannel.getId())) | ||
.filter(botTopic -> Objects.nonNull(botTopic.getName())) | ||
.collect(Collectors.toList()); | ||
guildConfig.setTopicOrChannelList(botTopicList); | ||
}); | ||
} catch (InterruptedException e) { | ||
log.error("Init Discord bot error.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.append("botServerConfig", botServerConfig) | ||
.append("bot", bot) | ||
.toString(); | ||
} | ||
} |
65 changes: 0 additions & 65 deletions
65
src/main/java/com/dl/officialsite/bot/discord/DiscordBotConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 12 additions & 24 deletions
36
...officialsite/bot/model/BaseBotConfig.java → ...om/dl/officialsite/bot/model/BaseBot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.