Browse Source

Add Role Kiosk Functionality

master
parent
commit
753d66488c
No known key found for this signature in database GPG Key ID: DA34C790D267C164
7 changed files with 123 additions and 6 deletions
  1. +1
    -0
      README.md
  2. +3
    -3
      shard.lock
  3. +2
    -2
      shard.yml
  4. +5
    -0
      src/Bampersand.cr
  5. +31
    -0
      src/commands/kiosk.cr
  6. +1
    -1
      src/modules/Killfile.cr
  7. +80
    -0
      src/modules/RoleKiosk.cr

+ 1
- 0
README.md View File

@ -12,6 +12,7 @@ Adapt `.env.example` to your needs and rename it to `.env`. It's gitignored by d
5. `create table warnings (guild_id unsigned integer, user_id unsigned integer, mod_id unsigned integer, text string, timestamp date default current_timestamp);`
6. `create table perms (guild_id unsigned integer unique on conflict replace, admin_id unsigned integer, moderator_id unsigned integer);`
7. `create table killfile (guild_id unique on conflict replace);`
8. `create table role_kiosks (message_id unsigned integer unique on conflict replace, data string);`
## Contributing


+ 3
- 3
shard.lock View File

@ -2,11 +2,11 @@ version: 1.0
shards:
db:
github: crystal-lang/crystal-db
version: 0.5.1
version: 0.6.0
discordcr:
github: z64/discordcr
commit: 1231089bea30779b2398629745e6e99784e2301d
commit: b667b7f770582bc339c332ee3581820b5fbb2804
dotenv:
github: gdotdesign/cr-dotenv
@ -14,5 +14,5 @@ shards:
sqlite3:
github: crystal-lang/crystal-sqlite3
version: 0.12.0
version: 0.13.0

+ 2
- 2
shard.yml View File

@ -1,5 +1,5 @@
name: Bampersand
version: 0.16.0
version: 0.17.0
authors:
- deing <admin@15318.de>
@ -18,6 +18,6 @@ dependencies:
github: crystal-lang/crystal-sqlite3
dotenv:
github: gdotdesign/cr-dotenv
crystal: 0.27.2
crystal: 0.30.0
license: MIT

+ 5
- 0
src/Bampersand.cr View File

@ -29,6 +29,7 @@ require "./modules/Board"
require "./modules/JoinLeaveLog"
require "./modules/ModTools"
require "./modules/Killfile"
require "./modules/RoleKiosk"
require "./modules/Commands"
module Bampersand
@ -81,8 +82,12 @@ module Bampersand
end
end
bot!.on_message_reaction_add do |payload|
RoleKiosk.handle_reaction_add(payload)
Board.handle_reaction(payload)
end
bot!.on_message_reaction_remove do |payload|
RoleKiosk.handle_reaction_remove(payload)
end
bot!.on_guild_create do |payload|
LOG.info(
"Joined new guild #{payload.name} [#{payload.id}] — Owner is #{payload.owner_id}"


+ 31
- 0
src/commands/kiosk.cr View File

@ -0,0 +1,31 @@
Commands.register_command("rolekiosk update", "Configure a message as a role kiosk.", Perms::Level::Admin) do |args|
Arguments.assert_count(args, 2)
RoleKiosk.update_kiosk(args[0].to_u64, args[1])
true
end
Commands.register_command("rolekiosk delete", "Disables a message's role kiosk functionality.", Perms::Level::Admin) do |args|
Arguments.assert_count(args, 1)
RoleKiosk.delete_kiosk(args[0].to_u64)
true
end
Commands.register_command("rolekiosk info", "Displays a role kiosk's current configuration.", Perms::Level::Admin) do |args|
Arguments.assert_count(args, 1)
kiosk = RoleKiosk.kiosk(args[0].to_u64)
raise "Not configured for #{args[0]}" unless kiosk
{
title: "**ROLE KIOSK CONFIGURATION**",
text: "Message ID: `#{args[0]}`
#{kiosk.map { |x| "#{x[0]} <@&#{x[1]}>" }.join("\n")}",
}
end
Commands.register_command("rolekiosk", "[Manage Role Kiosks]", Perms::Level::Admin) do
{
text: "| rolekiosk update <message_id emoji|roleid;emoji|roleid;…>
| rolekiosk delete <message_id>
| rolekiosk info <message_id>",
title: "**BAMPERSAND ROLE KIOSK**",
}
end

+ 1
- 1
src/modules/Killfile.cr View File

@ -2,7 +2,7 @@ module Killfile
# This module handles guildwide self-blocks.
extend self
@@killfile: Array(UInt64) = load_killfile
@@killfile : Array(UInt64) = load_killfile
def load_killfile
killfile = [] of UInt64


+ 80
- 0
src/modules/RoleKiosk.cr View File

@ -0,0 +1,80 @@
module RoleKiosk
# This module handles role kiosks, allowing users to obtain roles
# by reacting to messages.
extend self
# Maps Message ID to Reaction string and associated RoleID
@@role_kiosks : Hash(UInt64, Hash(String, UInt64)) = load_kiosks
def load_kiosks
kiosks = {} of UInt64 => Hash(String, UInt64)
Bampersand::DATABASE.query(
"select message_id, data from role_kiosks"
) do |rs|
rs.each do
mid = rs.read(Int64).to_u64
data = rs.read(String)
emojis = [] of String
roles = [] of UInt64
data.split(";") { |arg|
split = arg.split("|")
emojis << split[0]
roles << split[1].to_u64
}
kiosks[mid] = Hash.zip(emojis, roles)
end
end
kiosks
end
def update_kiosk(message_id, data_string)
Bampersand::DATABASE.exec("insert into role_kiosks (message_id, data) values (?,?)", message_id.to_i64, data_string)
emojis = [] of String
roles = [] of UInt64
data_string.split(";") { |arg|
split = arg.split("|")
emojis << split[0]
roles << split[1].to_u64
}
@@role_kiosks[message_id] = Hash.zip(emojis, roles)
end
def kiosk(message_id)
@@role_kiosks[message_id]?
end
def delete_kiosk(message_id)
Bampersand::DATABASE.exec("delete from role_kiosks where message_id = ?", message_id.to_i64)
@@role_kiosks.delete(message_id)
end
def handle_reaction_add(payload)
return if cache!.resolve_user(payload.user_id).bot
lookup = @@role_kiosks[payload.message_id.to_u64]?
return unless lookup
target_role = lookup[Util.reaction_to_s(payload.emoji)]?
return unless target_role
LOG.info("Adding Role #{target_role} in #{payload.guild_id} to #{cache!.resolve_user(payload.user_id).tag}")
begin
bot!.add_guild_member_role(payload.guild_id.not_nil!.to_u64, payload.user_id.to_u64, target_role)
rescue e
LOG.error("Error while adding role: #{e}")
end
end
def handle_reaction_remove(payload)
return if cache!.resolve_user(payload.user_id).bot
lookup = @@role_kiosks[payload.message_id.to_u64]?
return unless lookup
target_role = lookup[Util.reaction_to_s(payload.emoji)]?
return unless target_role
LOG.info("Removing Role #{target_role} in #{payload.guild_id} from #{cache!.resolve_user(payload.user_id).tag}")
begin
bot!.remove_guild_member_role(payload.guild_id.not_nil!.to_u64, payload.user_id.to_u64, target_role)
rescue e
LOG.error("Error while removing role: #{e}")
end
end
LOG.info("Loaded RoleKiosk Module: #{@@role_kiosks.size} active kiosks")
end

Loading…
Cancel
Save