Skip to content

Commit

Permalink
Merge pull request #40 from tadeokondrak/devel-devops
Browse files Browse the repository at this point in the history
Added SelfRoles functionality and Rubocop compliance
  • Loading branch information
tadeokondrak authored Dec 3, 2017
2 parents 338678b + 128e881 commit 54c22f8
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 124 deletions.
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
inherit_from:
- https://raw.githubusercontent.com/meew0/discordrb/master/.rubocop.yml

Metrics/LineLength:
Max: 119

Style/GlobalVars:
AllowedVariables:
- $config
- $pasta
- $figlet
- $user_type
- $bot
14 changes: 7 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
source "https://rubygems.org" do
gem 'fullwidth'
gem 'discordrb'
gem 'figlet'
gem 'cowsay'
gem 'fortune_gem'
gem 'pry'
source 'https://rubygems.org' do
gem 'cowsay'
gem 'discordrb'
gem 'figlet'
gem 'fortune_gem'
gem 'fullwidth'
gem 'pry'
end
8 changes: 6 additions & 2 deletions config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ token:
serverID: ''
sudoersRole: ''
timeoutRole: ''
figletFont: '' #a font name from the fonts folder minus the .flf
selfRoles:
nonfsf: ''
botdev: ''
infoFile: ''
figletFont: '' # a font name from the fonts folder minus the .flf
prefix: ''
lusersList: '' #a sequence of stringos
lusersList: '' # a sequence of strings
43 changes: 20 additions & 23 deletions lib/admin.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
# Implements administration commands that are unavailable to regular users.
module Admin
extend Discordrb::Commands::CommandContainer

command(:clear,
description: "Clears N amount of messages. If no arguments are " \
"specified, 100 messages will be cleared. " \
"Requires superuser",
help_available: false) \
do |event, *args|
unless $userType != 'sudoer'
event.channel.send_message("Invalid amount") &&
break if args[0].to_i > 100
toDelete = event.channel.history(args.empty? ? 100 : args[0].to_i+1)
event.channel.delete_messages(toDelete)
"Clearing..."
description: 'Clears N amount of messages. If no arguments are ' \
'specified, 100 messages will be cleared. ' \
'Requires superuser',
help_available: false) do |event, *args|
if $user_type == 'sudoer'
event.channel.send_message('Invalid amount') && break if args[0].to_i > 100
to_delete = event.channel.history(args.empty? ? 100 : args[0].to_i + 1)
event.channel.delete_messages(to_delete)
else
"Permission denied."
'Permission denied.'
end
end

command(:timeout,
description: "Times out a user for N seconds. Requires superuser",
description: 'Times out a user for N seconds. Requires superuser',
min_args: 2,
usage: "[user to timeout] [seconds of timeout]",
help_available: false) \
do |event, *args|
unless $userType != 'sudoer'
timeOut = fork do
usage: '[user to timeout] [seconds of timeout]',
help_available: false) do |event, *args|
if $user_type == 'sudoer'
time_out = fork do
user = event.message.mentions[0].on($config['serverID'])
user.add_role($config['timeoutRole'])
sleep args[1].to_i
user.remove_role($config['timeoutRole'])
"#{user.name} timed out for #{args[1].to_i}"
event.channel.send_message("#{user.name} timed out for #{args[1].to_i}")
end
puts Process.detach(timeOut) #This puts is imporant so the thread ID
else #Doesn't get outputted by the bot
"Permission denied."
Process.detach(time_out)
return
else
'Permission denied.'
end

end
end
79 changes: 36 additions & 43 deletions lib/fun.rb
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
# Implements fluff commands that serve no real use
module Fun
extend Discordrb::Commands::CommandContainer

command(:aesthic,
description: "Makes a message A E S T H E T I C",
usage: "[text to convert]",
min_args: 1) \
do |event, *args|
description: 'Makes a message A E S T H E T I C',
usage: '[text to convert]',
min_args: 1) do |_event, *args|
args.join(' ').to_fullwidth
end

command(:step,
description: "Show your soles to someone or something",
usage: "[text]",
min_args: 1) \
do |event, *args|
description: 'Show your soles to someone or something',
usage: '[text]',
min_args: 1) do |event, *args|
args.to_a.delete_at(0) if args.include?('on')
"<@#{event.author.id.to_s}> steps on #{args.join(' ')}"
"<@#{event.author.id}> steps on #{args.join(' ')}"
end

command(:slap,
description: "Hit someone or something",
usage: "[text]") \
do |event, *args|
event.channel.send_message("<@#{event::author::id.to_s}> slaps " \
"#{args.join(' ')} around a bit with a large trout")
description: 'Hit someone or something',
usage: '[text]') do |event, *args|
"<@#{event.author.id}> slaps #{args.join(' ')} around a bit with a large trout"
end

command(:figlet,
description: "Turns a message into big ASCII art letters",
usage: "[text]",
min_args: 1) \
do |event, *args|
"```" + $figlet[args.join('')] + "```"
description: 'Turns a message into big ASCII art letters',
usage: '[text]',
min_args: 1) do |_event, *args|
'```' + $figlet[args.join('')] + '```'
end

command(:cowsay,
description: "Wraps a message with an ASCII art cow",
usage: "[text]",
min_args: 1) \
do |event, *args|
inputtext = args.join(' ').gsub("```", '');
renderedtext = Cowsay.say(inputtext, 'cow')
chunkedtext = ""
until renderedtext.empty?
sliced = renderedtext.slice(0..1990)
lastindex = sliced.length - 1
lastline = sliced.lines.last
unless lastline.end_with? '\n' or renderedtext.length == sliced.length
lastindex -= lastline.length
description: 'Wraps a message with an ASCII art cow',
usage: '[text]',
min_args: 1) do |_event, *args|
input_text = args.join(' ').gsub('```', '')
rendered_text = Cowsay.say(input_text, 'cow')
chunked_text = ''
until rendered_text.empty?
sliced = rendered_text.slice(0..1990)
last_index = sliced.length - 1
last_line = sliced.lines.last
unless last_line.end_with?('\n') || rendered_text.length == sliced.length
last_index -= last_line.length
end
chunkedtext << "```\n" + renderedtext.slice!(0..lastindex)+ "\n```" \
+ " " * lastline.length + "\n"
chunked_text << "```\n" + rendered_text.slice!(0..last_index) + "\n```" \
+ ' ' * last_line.length + "\n"
end
chunkedtext
chunked_text
end

command(:me,
description: "Executes a self action",
usage: "[text]",
min_args: 1) \
do |event, *args|
description: 'Executes a self action',
usage: '[text]',
min_args: 1) do |event, *args|
event.channel.delete_message(event.channel.history(1)[0])
"<@#{event.user.id.to_s}> *#{args.join(' ')}*"
"<@#{event.user.id}> *#{args.join(' ')}*"
end

command(:fortune,
description: "Outputs a random fortune / divination / bad joke") \
do |event|
"```" + FortuneGem.give_fortune + "```"
description: 'Outputs a random fortune / divination / bad joke') do |_event|
'```' + FortuneGem.give_fortune + '```'
end
end
9 changes: 5 additions & 4 deletions lib/patches.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#allow output from commands to exceed 2000 chars
# Allow output from commands to exceed 2000 chars
module Discordrb::Commands
# Overwrite of the CommandBot to monkey patch command length
class CommandBot
def execute_chain(chain, event)
def execute_chain(chain, event)
t = Thread.new do
@event_threads << t
Thread.current[:discordrb_name] = "ct-#{@current_thread += 1}"
begin
debug("Parsing command chain #{chain}")
result = @attributes[:advanced_functionality] ? CommandChain.new(chain, self).execute(event) : simple_execute(chain, event)
result = event.drain_into(result)

if event.file
event.send_file(event.file, caption: result)
else
unless result.nil? || result.empty?
Discordrb.split_message(result).each do |chunk|
event.respond chunk
end
end
end
end
rescue => e
Expand Down
54 changes: 34 additions & 20 deletions lib/utilities.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,68 @@
# Implements utility commands for everyone's use
module Utilities
extend Discordrb::Commands::CommandContainer

command(:random,
description: "Picks a random number.",
usage: "<min> <max>",
min_args: 2) \
do |event|
description: 'Picks a random number.',
usage: '<min> <max>',
min_args: 2) do |event|
# Parse the message and keep all parts but the command
# ["::random 1 2 4 5 -5"] => ["1", "2", "4", "5", "-5"]
args = (event.message.content.split (' '))[1 .. -1]
args = event.message.content.split(' ')[1..-1]

# Args to integer
input = Array.new(args.size){ |i| args[i].to_i }
input = Array.new(args.size) { |i| args[i].to_i }

case input.size
when 0
# Integer "limits" (there's no such thing in ruby though)
min = -(2**(0.size * 8 -2))
min = -(2**(0.size * 8 - 2))
max = min.abs
when 1
min = 0
max = 0
# If one args is given, the result will be of the same sign as the arg
input[0] >= 0? max = input[0] : min = input[0]
input[0] >= 0 ? max = input[0] : min = input[0]
else
min = input.min
max = input.max
end
rand(min .. max)
rand(min..max)
end

command(:echo,
description: "Echoes text.",
usage: "[text to echo]",
min_args: 1) \
do |event, *args|
description: 'Echoes text.',
usage: '[text to echo]',
min_args: 1) do |_event, *args|
args.join(' ')
end

command(:lusers,
description: "Prints the amount of lusers currently online.",
usage: "!lusers") \
do |event|
"Amount of lusers currently #{$config['lusersList'].sample}: #{event.server.online_users(include_idle: true).length.to_s}"
description: 'Prints the amount of lusers currently online.',
usage: '!lusers') do |event|
"Amount of lusers currently #{$config['lusersList'].sample}:" \
"#{event.server.online_users(include_idle: true).length}"
end

command(:checksudo,
description: "Prints if you're a sudoer",
usage: "!checksudo") \
do |event|
"You are a #{event.user.roles.include?($config['sudoersRole'])?'sudoer':'regular user'}."
usage: '!checksudo') do |event|
"You are a #{event.user.roles.include?($config['sudoersRole']) ? 'sudoer' : 'regular user'}."
end

command(:getrole,
description: 'Adds a self-assignable role',
usage: '<role to request>',
max_args: 1) do |event, *args|
role = args.join('')
if $config['selfRoles'].key?(role)
if event.author.role?(role)
'This user has already assigned that role to himself!'
end
event.author.add_role($config['selfRoles'][role])
"<@#{event.author.id}> was assigned the role #{role}!"
else
"Error: role #{role} does not exist.\nAvailable roles are: $config['selfRoles'].keys.join(', ')"
end
end
end
Loading

0 comments on commit 54c22f8

Please sign in to comment.