Skip to content

Commit

Permalink
FEATURE: added event listening stub area and example
Browse files Browse the repository at this point in the history
  • Loading branch information
merefield committed Feb 15, 2021
1 parent d07aa17 commit 9f8498c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
12 changes: 7 additions & 5 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
en:
site_settings:
discord_bot_enabled: "Enable the Discourse Discord bot plugin"
discord_bot_token: "Enter your bot TOKEN from Discord here"
discord_bot_admin_channel_id: "The id of your admin text channel on Discord"
discord_bot_admin_role_id: "The id of your admin role on Discord for which commands are permitted"
discord_bot_rate_limit_delay: "The delay in seconds between sending commands to Discord so we don't annihilate rate limits"
discord_bot_enabled: "Enable the Discourse Discord bot plugin"
discord_bot_token: "Enter your bot TOKEN from Discord here"
discord_bot_admin_channel_id: "The id of your admin text channel on Discord"
discord_bot_admin_role_id: "The id of your admin role on Discord for which commands are permitted"
discord_bot_rate_limit_delay: "The delay in seconds between sending commands to Discord so we don't annihilate rate limits"
discord_bot_post_announcement_categories: "Announce new Posts in here within the admin channel"
discord_bot_topic_announcement_categories: "Announce new Topics in here within the admin channel"
10 changes: 10 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ plugins:
discord_bot_admin_role_id:
default: ''
client: false
discord_bot_post_announcement_categories:
client: false
default: ''
type: category_list
list_type: "compact"
discord_bot_topic_announcement_categories:
client: false
default: ''
type: category_list
list_type: "compact"
discord_bot_rate_limit_delay:
default: 2
client: false
5 changes: 3 additions & 2 deletions lib/bot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Discord bot class
class DiscordBot
class ::DiscordBot::Bot

@@DiscordBot = nil

Expand All @@ -22,7 +22,8 @@ def self.run_bot
bot = self.init

unless bot.nil?
BotCommands.manage_commands(bot)
::DiscordBot::DiscourseEventsHandlers.hook_events
::DiscordBot::BotCommands.manage_discord_commands(bot)
end
end
end
4 changes: 2 additions & 2 deletions lib/bot_commands.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module BotCommands
def self.manage_commands(bot)
module ::DiscordBot::BotCommands
def self.manage_discord_commands(bot)
bot.bucket :admin_tasks, limit: 3, time_span: 60, delay: 10

# '!disckick' - a command to kick members beneath a certain trust level on Discourse
Expand Down
21 changes: 21 additions & 0 deletions lib/discourse_events_handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ::DiscordBot::DiscourseEventsHandlers
def self.hook_events
DiscourseEvent.on(:post_created) do |post|
if post.id > 0 && !::DiscordBot::Bot.discord_bot.nil? then
post_listening_categories = SiteSetting.discord_bot_post_announcement_categories.split('|')
topic_listening_categories = SiteSetting.discord_bot_topic_announcement_categories.split('|')
posted_category = post.topic.category.id
posted_category_name = Category.find_by(id: posted_category).name
if post_listening_categories.include?(posted_category.to_s) then
message = "There's a new Post in the '#{posted_category_name}' Category on Discourse: #{Discourse.base_url + post.url}"
::DiscordBot::Bot.discord_bot.send_message(SiteSetting.discord_bot_admin_channel_id, message)
else
if topic_listening_categories.include?(posted_category.to_s) && post.post_number = 1 then
message = "There's a new Topic in the '#{posted_category_name}' Category on Discourse: #{Discourse.base_url + post.url}"
::DiscordBot::Bot.discord_bot.send_message(SiteSetting.discord_bot_admin_channel_id, message)
end
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ::DiscordBot
class Engine < ::Rails::Engine
engine_name "discord_bot"
isolate_namespace DiscordBot
end
end
6 changes: 3 additions & 3 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@

enabled_site_setting :discord_bot_enabled

register_asset 'stylesheets/common/discord.scss'

after_initialize do

%w[
../lib/engine.rb
../lib/bot.rb
../lib/bot_commands.rb
../lib/discourse_events_handlers.rb
].each do |path|
load File.expand_path(path, __FILE__)
end

bot_thread = Thread.new do
begin
DiscordBot.run_bot
::DiscordBot::Bot.run_bot
rescue Exception => ex
Rails.logger.error("Discord Bot: There was a problem: #{ex}")
end
Expand Down

0 comments on commit 9f8498c

Please sign in to comment.