· 9 years ago · Dec 21, 2016, 02:18 PM
1#!/home/atlas/.rbenv/shims/ruby
2
3require 'bundler/setup'
4require 'discordrb'
5require 'yaml'
6require 'sqlite3'
7require 'json'
8
9
10module BruhBot
11 $LOAD_PATH << File.join(File.dirname(__FILE__))
12
13 require('module.rb')
14
15 $bot_conf = YAML::load_file('config.yml')
16 $api = YAML::load_file('apikeys.yml')
17
18 Dir.mkdir('db') unless File.exists?('db')
19 Dir.mkdir('logs') unless File.exists?('logs')
20 Dir.mkdir('logs/commandlogs') unless File.exists?('logs/commandlogs')
21 Dir.mkdir('logs/messagelogs') unless File.exists?('logs/messagelogs')
22
23 bot = Discordrb::Commands::CommandBot.new token: $api['discordtoken'], client_id: $api['discordappid'], prefix: $bot_conf['prefix']
24
25 db = SQLite3::Database.new 'db/server.db'
26 db.execute <<-SQL
27 create table if not exists perms (
28 command text,
29 roles text,
30 UNIQUE(command)
31 );
32 SQL
33
34 db.execute('INSERT OR IGNORE INTO perms (command) VALUES (?), (?), (?), (?), (?), (?)', ('update'), ('restart'), ('shutdown'), ('game'), ('clear'), ('roles'))
35 shutdown_string = db.execute( 'SELECT roles FROM perms WHERE command = ?', ('shutdown'))[0][0]
36 shutdown_roles = shutdown_string.split(',').map(&:to_i) unless (shutdown_string == nil)
37
38 Dir['plugins/*/*.rb'].each { |file| require file }
39 (Plugins.all_the_modules-[Plugins]).each do |plugin|
40 bot.include! plugin
41 end
42
43 bot.command(:test, min_args: 0, max_args: 0, required_roles: [], description: 'Update the bot.', usage: 'update') do |event|
44 db = SQLite3::Database.new 'db/server.db'
45 roles = db.execute( 'SELECT * FROM perms' )
46 puts roles
47 end
48
49 bot.command(%s(perm.add), min_args: 1, required_roles: [], description: 'Update the bot.', usage: 'update') do |event, command, *roles|
50 db = SQLite3::Database.new 'db/server.db'
51 role_array = db.execute( 'SELECT roles FROM perms WHERE command = ?', (command))[0][0]
52 role_array = role_array.split(' ').map(&:to_i) unless (role_array == nil)
53 role_array = [] unless (role_array != nil)
54 output = ''
55 good_output = ''
56 roles.each do |role|
57 next event.respond 'You have to add roles, and not users.' if (event.server.member(role.to_i) != nil)
58 role_id = event.server.role(role.to_i)
59 if (role_array.include?(role.to_i) == false)
60 role_array << role.to_i
61 good_output += "The following users and roles were given this permission:\n\n" unless (good_output != '')
62 good_output += "#{role_id.name}\n"
63 elsif (role_array.include? role.to_i)
64 output += "The following users and roles already have this permission:\n\n" unless (output != '')
65 output += "#{role_id.name}\n"
66 end
67 end
68 db.execute('UPDATE perms SET roles = (?) WHERE command = (?)', (role_array.join(',')), (command)) unless (role_array.empty?)
69 if (good_output != '')
70 event.channel.send_embed do |e|
71 #e.thumbnail = { url: currency_config['embed_image'] }
72 e.description = good_output
73 #e.add_field name: 'Money:', value: "#{currency_config['currency_symbol']}#{displayamount} #{currency_config['currency_name']}", inline: false
74 #e.color = currency_config['embed_color']
75 end
76 end
77 if (output != '')
78 event.channel.send_embed do |e|
79 #e.thumbnail = { url: currency_config['embed_image'] }
80 e.description = output
81 #e.add_field name: 'Money:', value: "#{currency_config['currency_symbol']}#{displayamount} #{currency_config['currency_name']}", inline: false
82 #e.color = currency_config['embed_color']
83 end
84 end
85 nil
86 end
87
88 bot.command(%s(perm.remove), min_args: 1, required_roles: [], description: 'Remove permissions from a command.', usage: 'perm.remove <command> <user1> <user2> <role1> <role2>') do |event, command, *roles|
89 db = SQLite3::Database.new 'db/server.db'
90 role_array = db.execute( 'SELECT (roles) FROM perms WHERE command = ?', (command))[0][0]
91 role_array = role_array.split(' ') unless (role_array == nil)
92 #role_array = [] unless (role_array != nil)
93 output = ''
94 good_output = ''
95 puts roles
96 roles.each do |role|
97 puts 'in each'
98 if (role_array.include?(role) == false)
99 role_array << role
100 good_output += "The following users and roles were given this permission:\n\n" unless (good_output != '')
101 member = event.server.member(role)
102 userrole = event.server.role(role)
103 good_output += "#{member.display_name}\n" unless (member == nil)
104 good_output += "#{userrole.name}\n" unless (userrole == nil)
105 puts 'not poop'
106 elsif (role_array.include? role)
107 output += "The following users and roles already have this permission:\n\n" unless (output != '')
108 member = event.server.member(role)
109 userrole = event.server.role(role)
110 output += "#{member.display_name}\n" unless (member == nil)
111 output += "#{userrole.name}\n" unless (userrole == nil)
112 puts 'poop'
113 end
114 end
115 puts 'after each'
116 puts role_array
117 db.execute('UPDATE perms SET roles = (?) WHERE command = (?)', (role_array.join(' ')), (command))
118 if (good_output != '')
119 event.channel.send_embed do |e|
120 #e.thumbnail = { url: currency_config['embed_image'] }
121 e.description = good_output
122 #e.add_field name: 'Money:', value: "#{currency_config['currency_symbol']}#{displayamount} #{currency_config['currency_name']}", inline: false
123 #e.color = currency_config['embed_color']
124 end
125 elsif (output != '')
126 event.channel.send_embed do |e|
127 #e.thumbnail = { url: currency_config['embed_image'] }
128 e.description = output
129 #e.add_field name: 'Money:', value: "#{currency_config['currency_symbol']}#{displayamount} #{currency_config['currency_name']}", inline: false
130 #e.color = currency_config['embed_color']
131 end
132 end
133 nil
134 end
135
136 bot.command(:emoji_id, min_args: 0, required_roles: [], description: 'Update the bot.', usage: 'update') do |event|
137 event.respond "``` #{event.message}```"
138 end
139
140 bot.command(:update, min_args: 0, max_args: 0, required_roles: [], description: 'Update the bot.', usage: 'update') do |event|
141 if $bot_conf['server_protection'] == 1
142 break unless $bot_conf['owners'].include? event.user.id
143 end
144 event.respond 'Updating and restarting!'
145 exec('update.sh')
146 end
147
148 bot.command(:restart, min_args: 0, max_args: 0, required_roles: [], description: 'Restart the bot.', usage: 'restart') do |event|
149 if $bot_conf['server_protection'] == 1
150 break unless $bot_conf['owners'].include? event.user.id
151 end
152 event.respond 'Restarting!'
153 exec("#{File.expand_path File.dirname(__FILE__)}/update.sh restart")
154 end
155
156 bot.command(:shutdown, required_roles: shutdown_roles, help_available: false) do |event|
157 if $bot_conf['server_protection'] == 1
158 break unless $bot_conf['owners'].include? event.user.id
159 end
160 event.respond $bot_conf['shutdownmessage'].sample
161 event.bot.stop
162 end
163
164 bot.command(:nick, min_args: 1, required_roles: [], description: 'Change your nickname.', usage: 'nick <text>') do |event, *nick|
165 nick = nick.join(' ')
166 event.bot.member(event.server.id, event.user.id).nick = nick
167 nil
168 end
169
170 bot.command(%s(nick.user), min_args: 2, required_roles: [], description: 'Change a user\'s nickname.', usage: 'nick <user id> <text>') do |event, userid, *nick|
171 if $bot_conf['server_protection'] == 1
172 break unless $bot_conf['owners'].include? event.user.id
173 end
174 nick = nick.join(' ')
175 event.bot.member(event.server.id, userid).nick = nick
176 nil
177 end
178
179 bot.command(:game, min_args: 1, required_roles: [], description: 'sets bot game') do |event, *game|
180 if $bot_conf['server_protection'] == 1
181 break unless $bot_conf['owners'].include? event.user.id
182 end
183 event.bot.game = game.join(' ')
184 nil
185 end
186
187 bot.command(:clear, min_args: 1, max_args: 1, required_roles: [], description: 'Prune X messages from channel') do |event, number|
188 if $bot_conf['server_protection'] == 1
189 break unless $bot_conf['owners'].include? event.user.id
190 end
191 event.message.delete
192 if (/\A\d+\z/.match(number) != nil)
193 event.channel.prune(number.to_i)
194 stuff = event.respond("[#{number.to_i}] messages cleared.").id
195 sleep 5
196 event.channel.load_message(stuff).delete
197 else
198 event.respond('Please enter a valid number.')
199 end
200 end
201
202 bot.command(:roles, min_args: 0, max_args: 0, required_roles: [], description: 'Get info on all the roles on the server.') do |event|
203 roles = event.server.roles
204 output = '```'
205 roles.each do |role|
206 output += "Name:#{role.name}, ID:#{role.id}, Permissions:#{role.permissions.bits}\n"
207 if output.length >= 1800
208 output += '```'
209 event.user.pm(output)
210 output = '```'
211 end
212 end
213 output += '```'
214 event.user.pm(output)
215 nil
216 end
217
218 # Here we output the invite URL to the console so the bot account can be invited to the channel.
219 puts "This bot's invite URL is #{bot.invite_url}&permissions=261120"
220 puts 'Click on it to invite it to your server.'
221
222 if File.exists?('plugins/update.txt')
223 File.delete('plugins/update.txt')
224 end
225
226 bot.run
227end