-
Notifications
You must be signed in to change notification settings - Fork 94
/
commands.rb
54 lines (43 loc) · 2 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
# This bot has various commands that show off CommandBot.
require 'discordrb'
# Here we instantiate a `CommandBot` instead of a regular `Bot`, which has the functionality to add commands using the
# `command` method. We have to set a `prefix` here, which will be the character that triggers command execution.
bot = Discordrb::Commands::CommandBot.new token: 'B0T.T0KEN.here', prefix: '!'
bot.command :user do |event|
# Commands send whatever is returned from the block to the channel. This allows for compact commands like this,
# but you have to be aware of this so you don't accidentally return something you didn't intend to.
# To prevent the return value to be sent to the channel, you can just return `nil`.
event.user.name
end
bot.command :bold do |_event, *args|
# Again, the return value of the block is sent to the channel
"**#{args.join(' ')}**"
end
bot.command :italic do |_event, *args|
"*#{args.join(' ')}*"
end
bot.command(:invite, chain_usable: false) do |event|
# This simply sends the bot's invite URL, without any specific permissions,
# to the channel.
event.bot.invite_url
end
bot.command(:random, min_args: 0, max_args: 2, description: 'Generates a random number between 0 and 1, 0 and max or min and max.', usage: 'random [min/max] [max]') do |_event, min, max|
# The `if` statement returns one of multiple different things based on the condition. Its return value
# is then returned from the block and sent to the channel
if max
rand(min.to_i..max.to_i)
elsif min
rand(0..min.to_i)
else
rand
end
end
bot.command :long do |event|
event << 'This is a long message.'
event << 'It has multiple lines that are each sent by doing `event << line`.'
event << 'This is an easy way to do such long messages, or to create lines that should only be sent conditionally.'
event << 'Anyway, have a nice day.'
# Here we don't have to worry about the return value because the `event << line` statement automatically returns nil.
end
bot.run