Skip to content

Commit

Permalink
Merge pull request #12 from badBlackShark/develop
Browse files Browse the repository at this point in the history
v1.3.1
  • Loading branch information
badBlackShark authored May 10, 2018
2 parents d29a960 + a6eda45 commit 2f1c763
Show file tree
Hide file tree
Showing 24 changed files with 846 additions and 136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ april_fools.rb
Gemfile.lock
testbot_invite.txt
db_test.rb
mute_dump.txt
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ gem 'sequel'

# Charts
gem 'gruff', git: 'https://github.com/badBlackShark/gruff'

# Scheduling
gem 'rufus-scheduler'
1 change: 1 addition & 0 deletions lib/charts/game_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def generate_data(games)
games.sort.each_with_index do |(game, count), i|
count_array = Array.new(i, 0)
count_array.push(count)
game = game.length > 30 ? game[0, game.rindex(/\s/, 30)].rstrip << '...' : game
@chart.data(game.to_sym, count_array, '#06AAF5')
@chart.labels[i] = game unless @chart.labels.include?(game)
end
Expand Down
37 changes: 24 additions & 13 deletions lib/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def initialize(server: nil, username: nil, server_password: nil, db_name: nil, d
def create_table(table_name, columns = {})
@database.create_table?(table_name) do
columns.each do |column_name, datatype|
# Not pretty, but the only way to actually get it to store IDs correctly.
datatype == Integer ? (Bignum column_name) : (String column_name)
column column_name, datatype
end
end
end
Expand All @@ -42,15 +41,6 @@ def close
@gateway.shutdown!
end

# Outputs everything on the database related to the server. Just for debugging.
def test_output(table_name)
puts @database[table_name].select.all
end

def schema(table_name)
puts @database.schema(table_name)
end

# Returns all the non-nil values of a column as an array.
def read_column(table_name, column)
@database[table_name].select(column).map(&:values).flatten.compact
Expand Down Expand Up @@ -78,7 +68,6 @@ def update_string_value(table_name, column, value)

# Inserts value if it doesn't exist for given server ID.
# Returns true if the insert was successful, false if a duplicate was found
# Look into what some guy said on the discord API server to maybe safe an "if" here
def unique_insert(table_name, column, value)
if read_column(table_name, column).include?(value)
false
Expand All @@ -88,7 +77,8 @@ def unique_insert(table_name, column, value)
end
end

# Removes the row with a given server ID and a given value in a given column.
# Removes the row with a given value in a given column.
# Returns whether or not something was actually deleted.
def delete_value(table_name, column, value)
if read_column(table_name, column).include?(value)
@database[table_name].where(column => value).delete
Expand All @@ -103,6 +93,27 @@ def delete_server_values(server_id)
@database.drop_table?("shrk_server_#{server_id}".to_sym)
end

# Insert a row into a table (assumes you know what the columns are!)
def insert_row(table_name, values)
@database[table_name].insert(values)
end

# Select rows, based on one filter column
def select_rows(table_name, column, value)
@database[table_name].where(column => value).all
end

# Updates a row. The first value is assumed to be the primary key.
def update_row(table_name, values)
@database[table_name].where(@database[table_name].columns.first => values.first).delete
@database[table_name].insert(values)
end

# Returns the whole table
def read_all(table_name)
@database[table_name].all
end

# Sets the default values for the log- and assignment-channel, if they don't already have values.
def init_default_values(server)
LOGGER.init_log_channel(server)
Expand Down
1 change: 1 addition & 0 deletions lib/emojis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Emojis
'heart' => { unicode: "\u2764", emoji: '❤' },
'checkmark' => { unicode: "\u2705", emoji: '✅' },
'crossmark' => { unicode: "\u274C", emoji: '❌' },
'thumbs_up' => { unicode: "\u1F44D", emoji: '👍' },
'0' => { unicode: "\u0030\u20E3", emoji: '0⃣' },
'1' => { unicode: "\u0031\u20E3", emoji: '1⃣' },
'2' => { unicode: "\u0032\u20E3", emoji: '2⃣' },
Expand Down
31 changes: 31 additions & 0 deletions lib/reactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,35 @@ def self.confirm(message)
def self.error(message)
react(message, Emojis.name_to_unicode('crossmark'))
end

# Used to put an accept / decline dialog on a message. Gets the user that the prompt is for.
# Returns true / false depending on input, or nil if no choice was made.
# Staff users (permission level 1) are also able to make the choice.
def self.yes_no(message, user)
choice = nil

SHRK.add_await(:"yes_no_#{message.id}", Discordrb::Events::ReactionAddEvent) do |r_event|
# Only the user who sent the message and staff should be abled to confirm / deny.
next false unless (r_event.message.id == message.id) && (r_event.user.id == user.id || SHRK.permission?(event.user, 1, event.server))
choice = false if r_event.emoji.name == Emojis.name_to_emoji('crossmark')
choice = true if r_event.emoji.name == Emojis.name_to_emoji('checkmark')

next false if choice.nil?
end
spam_reactions(message, [Emojis.name_to_emoji('checkmark'), Emojis.name_to_emoji('crossmark')])

# Timeout
i = 30
loop do
if i.zero?
SHRK.awaits.delete(:"yes_no_#{message.id}")
return nil
elsif choice.nil?
i -= 1
sleep 1
else
return choice
end
end
end
end
7 changes: 3 additions & 4 deletions lib/role_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ def self.refresh_reactions(server)
end

def self.add_role_await(server, message)
SHRK.add_await(:"roles_#{message.id}", Discordrb::Events::ReactionEvent) do |event|
SHRK.add_await(:"roles_#{message.id}", Discordrb::Events::ReactionAddEvent) do |event|
next false unless event.message.id == message.id
# Reaction events are broken, needs the check to make sure it's actually the event I want.
next false unless event.class == Discordrb::Events::ReactionAddEvent
next false if event.user.id == BOT_ID

role_id = emoji_to_role_id(server, event.emoji.name)
Expand All @@ -65,8 +64,8 @@ def self.add_role_await(server, message)
if (sec_left = @assignment_bucket.rate_limited?(event.user))
time_left = "#{sec_left.to_i / 60} minutes and #{sec_left.to_i % 60} seconds"
event.user.pm("You can't assign yourself another role for #{time_left}.")
LOGGER.log("#{event.user.distinct} tried to give himself "\
"#{id_to_role(event.server, role_id)}\", but still has #{time_left} cooldown.")
LOGGER.log(event.server, "#{event.user.distinct} tried to give himself the role "\
"**#{id_to_role(event.server, role_id)}**, but still has **#{time_left}** cooldown.")
next false
end
add_role_to_user(event.user, server, role_id)
Expand Down
6 changes: 3 additions & 3 deletions lib/shrk_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ def log(server, message)
log_channel = get_log_channel(server.id)

unless log_channel
# Format message as code, to distinguish between the original and the added warning message.
message.prepend('```') << '```'
# Format message as bold, to distinguish between the original and the added warning message.
message.prepend("#{'-'*150}\n**") << "**\n#{'-'*150}\n"
# Add a warning message.
message << "\nCouldn't find a log channel to log this message to on \"#{server.name}.\" "
message << "Couldn't find a log channel to log this message to on \"#{server.name}.\" "
message << 'Please set one by using the `setLogChannel` command on that server.'
pm_owner(server, message)
return
Expand Down
43 changes: 25 additions & 18 deletions modules/assignment_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module AssignmentCommands
min_args: 1
}
# This is the manual setter, ServerSystem attempts to assign a default value when initializing.
command :setAssignmentChannel, attrs do |event, *args|
command :setassignmentchannel, attrs do |event, *args|
channel = event.server.channels.find { |s_channel| s_channel.name.casecmp?(args.join(' ')) }
next "That channel doesn't exist." unless channel

Expand All @@ -26,11 +26,11 @@ module AssignmentCommands
attrs = {
permission_level: 1,
permission_message: false,
usage: 'addToSelfAssign <roleName>',
description: 'Adds a role to the list of self-assignable roles.',
usage: 'addToSelfAssign <roleNames>',
description: 'Adds as many roles to the list of self-assignable roles as you want.',
min_args: 1
}
command :addToSelfAssign, attrs do |event, *args|
command :addtoselfassign, attrs do |event, *args|
# Name of the role => successfully inserted
roles = Hash[args.join(' ').split(', ').collect { |role_name| [role_name, false] }]
roles.each_key do |role_name|
Expand All @@ -44,27 +44,35 @@ module AssignmentCommands

response = roles.select { |_role_name, success| success }.keys.join('", "')

unless response == ''
unless response.empty?
RoleMessage.send!(event.server)
response.prepend('Added the roles "') << '" to the list of self-assignable roles.'
"Added the roles \"#{response}\" to the list of self-assignable roles."
end
end

attrs = {
permission_level: 1,
permission_message: false,
usage: 'removeFromSelfAssign <roleName>',
description: 'Removes a role from the list of self-assignable roles.',
description: 'Removes as many roles from the list of self-assignable roles as you want.',
min_args: 1
}
command :removeFromSelfAssign, attrs do |event, *args|
role = event.server.roles.find { |s_role| s_role.name.casecmp?(args.join(' ')) }
command :removefromselfassign, attrs do |event, *args|
roles = Hash[args.join(' ').split(', ').collect { |role_name| [role_name, false] }]
roles.each_key do |role_name|
role = event.server.roles.find { |s_role| s_role.name.casecmp?(role_name) }
if DB.delete_value("shrk_server_#{event.server.id}".to_sym, :roles, role.id)
roles[role_name] = true
else
event.respond("The role \"#{role_name}\" isn't self-assignable.")
end
end

response = roles.select { |_role_name, success| success }.keys.join('", "')

if DB.delete_value("shrk_server_#{event.server.id}".to_sym, :roles, role.id)
unless response.empty?
RoleMessage.send!(event.server)
event.message.react(Emojis.name_to_unicode('checkmark'))
else
event.send_temporary_message("The role #{args.join(' ').downcase} isn't self-assignable.", 10)
"Removed the roles \"#{response}\" from the list of self-assignable roles."
end
end

Expand All @@ -74,7 +82,7 @@ module AssignmentCommands
usage: 'refreshRoles',
description: 'Triggers a manual refresh for the role message.'
}
command :refreshRoles, attrs do |event|
command :refreshroles, attrs do |event|
RoleMessage.send!(event.server)
end

Expand All @@ -83,8 +91,8 @@ module AssignmentCommands
description: 'Points you to the channel where you can assign roles to yourself.'
}
command :roles, attrs do |event|
channel_id = DB.read_value("shrk_server_#{event.server.id}", :assignment_channel)
"In the channel <##{channel_id}> you can find the roles you can assign to yourself."
channel_id = DB.read_value("shrk_server_#{event.server.id}".to_sym, :assignment_channel)
"You can assign roles to yourself in <##{channel_id}>."
end

attrs = {
Expand All @@ -93,9 +101,8 @@ module AssignmentCommands
usage: 'assignmentChannel?',
description: 'Tells you what the assignment channel is, in case you forgot which one it was.'
}
command :assignmentChannel?, attrs do |event|
command :assignmentchannel?, attrs do |event|
assignment_channel = DB.read_value("shrk_server_#{event.server.id}".to_sym, :assignment_channel)

next "The assignment channel is <##{assignment_channel}>." if assignment_channel

'There is no assignment channel. Please set one by using the `setAssignmentChannel` command.'
Expand Down
14 changes: 8 additions & 6 deletions modules/chart_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module ChartCommands
extend Discordrb::Commands::CommandContainer

attrs = {
description: 'Generates a chart showing the distribution of roles on the server.',
usage: 'roleChart <minMembers> || Selects 10 roles max, if <min_members> isn\'t specified.'
usage: 'roleChart <minMembers>',
description: 'Generates a chart showing the distribution of roles on the server. '\
'Selects 10 roles max, if <min_members> isn\'t specified.'
}
command :roleChart, attrs do |event, min_members|
command :rolechart, attrs do |event, min_members|
event.send_temporary_message('Creating and uploading your chart. Hold on...', 10)

data = {}
Expand All @@ -29,10 +30,11 @@ module ChartCommands
end

attrs = {
description: 'Generates a chart of the games currently being played on the server.',
usage: 'gameChart'
usage: 'gameChart',
description: 'Generates a chart of the games currently being played on the server. '\
'The scale can currently be a bit weird, but the data is correct.'
}
command :gameChart, attrs do |event|
command :gamechart, attrs do |event|
event.send_temporary_message('Creating and uploading your chart. Hold on...', 10)

data = Hash.new(0)
Expand Down
9 changes: 9 additions & 0 deletions modules/fun_stuff.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Fun, trolly stuff goes in here
module FunStuff
extend Discordrb::EventContainer
extend Discordrb::Commands::CommandContainer

message(contains: /^lol$/i) do |event|
event.respond '- Rondo' if (1..100).to_a.sample == 1
end
end
Loading

0 comments on commit 2f1c763

Please sign in to comment.