-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1730 from elanthia-online/discordhook.lic-v1.0.0
[discordhook.lic] v.1.0.0 initial release
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
=begin | ||
A discord webhook module for Lich5. | ||
Set your webhook url via: | ||
LichDiscordHook.webhook="url here" | ||
Use the webhook via: | ||
LichDiscordHook.msg("message", title: "title", timestamp: Time.now, description: "whatever") | ||
Example One: | ||
description = | ||
"#{XMLData.room_title.sub(']', " - #{Room.current.id}]")} (u#{XMLData.room_id})\n" + | ||
reget(20).join | ||
LichDiscordHook.msg("\nLogging Out!", description: description) | ||
Example Two: | ||
LichDiscordHook.msg("\nFEEDER FOUND!\n```#{line}```") | ||
author: elanthia-online | ||
game: any | ||
tags: discord, webhooks | ||
version: 1.0.0 | ||
changelog: | ||
v1.0.0 - 2025-01-05 | ||
* initial release | ||
=end | ||
require "rubygems" | ||
require "rubygems/dependency_installer" | ||
installer = Gem::DependencyInstaller.new({ :user_install => true, :document => nil }) | ||
installed_gems = Gem::Specification.map { |gem| gem.name }.sort.uniq | ||
|
||
gems_to_install = ['discordrb'] | ||
failed_gems_install = [] | ||
gems_to_install.each { |gem| | ||
begin | ||
unless installed_gems.include?(gem) | ||
echo "Installing missing ruby gem '#{gem}' now, please wait!" | ||
installer.install(gem) | ||
echo "Done installing '#{gem}' gem!" | ||
end | ||
require gem.sub('-', '/') | ||
rescue | ||
failed_gems_install.push(gem) | ||
end | ||
} | ||
|
||
unless failed_gems_install.empty? | ||
echo "Required Ruby gems failed to install: #{failed_gems_install.join(', ')}" | ||
echo "Please install the above gem(s) to run ;#{Script.current.name}" | ||
exit | ||
end | ||
|
||
require 'discordrb/webhooks' | ||
|
||
module LichDiscordHook | ||
@webhook = '' | ||
|
||
def self.webhook | ||
if @webhook.empty? | ||
begin | ||
val = Lich.db.get_first_value("SELECT value FROM lich_settings WHERE name='discord_webhook';") | ||
rescue SQLite3::BusyException | ||
sleep 0.1 | ||
retry | ||
end | ||
val = '' if val.nil? | ||
@webhook = val if !val.nil? | ||
end | ||
return @webhook | ||
end | ||
|
||
def self.reload_hook | ||
@client = Discordrb::Webhooks::Client.new(url: self.webhook) | ||
end | ||
|
||
def self.webhook=(val) | ||
@webhook = val.to_s | ||
begin | ||
Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('discord_webhook',?);", [@webhook.to_s.encode('UTF-8')]) | ||
rescue SQLite3::BusyException | ||
sleep 0.1 | ||
retry | ||
end | ||
self.reload_hook | ||
return nil | ||
end | ||
|
||
@client = Discordrb::Webhooks::Client.new(url: self.webhook) | ||
|
||
def self.msg(message = '', title: "#{XMLData.game}:#{XMLData.name}", timestamp: Time.now, description: "") | ||
return if @webhook.empty? | ||
description = | ||
"#{XMLData.room_title.sub(']', " - #{Room.current.id}]")} (u#{XMLData.room_id})\n" + | ||
"Objects: #{GameObj.loot.map { |item| item.name }.join(', ')}\n\n" + | ||
"NPCs: #{GameObj.npcs.map { |npc| npc.name }.join(', ')}\n" + | ||
"Also here: #{GameObj.pcs.map { |pc| pc.noun }.join(', ')}\n" + | ||
"#{XMLData.room_exits_string}\n" if description.empty? | ||
@client.execute do |builder| | ||
builder.content = "#{message}" | ||
builder.add_embed do |embed| | ||
embed.title = title | ||
embed.description = description | ||
embed.timestamp = timestamp | ||
end | ||
end | ||
end | ||
end |