This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
/
channel_commands.go
123 lines (105 loc) · 3.55 KB
/
channel_commands.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package CommanD_Bot
import (
"errors"
"strconv"
)
type ChannelCommands struct {
commands map[string]func(*Root)error
}
func (m *ChannelCommands) RunCommand(root *Root) error {
return m.commands[root.CommandType()](root)
}
func LoadChannelCommand() *ChannelCommands {
// Creates channel command struct //
c := ChannelCommands{
make(map[string]func(*Root)error),
}
// Create Channel function //
c.commands["-new"] = CreateChannel
c.commands["-c"] = CreateChannel
// Delete Channel function //
c.commands["-delete"] = DeleteChannel
c.commands["-del"] = DeleteChannel
// Return reference to Channel commands instance //
return &c
}
// Create Channel command help data //
func loadChannelCommandInfo() *commandInfo {
// Create new commandInfo struct //
c := commandInfo{}
// Channel command default help info //
c.detail = "**!channel** or **!ch** : All commands that pertain to manipulating text and voice channels with in a server."
// Create sub command info map //
c.commands = make(map[string]string)
// Create Create Channel help info //
c.commands["-new"] = "**-new** or **-c**.\n" +
"**Info**: Creates a new channel in the guild.\n" +
"**Arguments:**\n" +
" **<channel name>**: Creates a text channel by default with given name.\n" +
" **<Channel Name><Channel Type>:** Creates a channel with given name and type (text or voice).\n"
// Create Delete Channel help info //
c.commands["-delete"] = "**-delete** or **-del**: Deletes a channel from the guild.\n" +
"**Arguments:**\n" +
" **<channel name>**: Deletes the channel with given name.\n"
// Return reference to Channel help info instance //
return &c
}
// Create new channel function //
// - Returns an error (nil if non)
func CreateChannel(root *Root) error {
// Check if user is admin //
// - Returns if user is not an admin
// - Returns an error if err is not nil
if ok, err := root.IsAdmin(); err != nil {
return err
} else if !ok {
return root.MessageSend("You do not have permission to do that.")
}
// Check the number of args //
// - 3 = Create a channel with out giving a type (default text)
// - 4 = Create a channel with a given type
switch len(root.CommandArgs()) {
case 1:
// Create channel with default type //
return root.NewChannel(root.CommandArgs()[0], "text")
case 2:
// Create channel with a given type //
return root.NewChannel(root.CommandArgs()[0], root.CommandArgs()[1])
default:
// Error if the number of arguments is anything above 4 or below 3 //
return errors.New("length of args was not correct. Length: " + strconv.Itoa(len(root.CommandArgs())))
}
return nil
}
// Delete a channel function //
// - Returns an error (nil if non)
func DeleteChannel(root *Root) error {
// Check if user is an admin //
// - returns an error if err is not nil
// - returns if user is not an admin
if ok, err := root.IsAdmin(); err != nil {
return err
} else if !ok {
return root.MessageSend( "You do not have permission to do that.")
}
// Check length of message //
// - returns an error if if args does not have a channel name
switch len(root.CommandArgs()) {
// channel name was given //
case 1:
// Get channel to delete //
// - returns an error if err is not nil
if c, err := root.GetChannelToDel(root.CommandArgs()[0]); err != nil {
return err
} else {
// Delete channel //
// - returns error (nil if non)
_, err := root.ChannelDelete(c.ID)
return err
}
// channel name was not given //
default:
// returns an error for channel name not being given //
return errors.New("channel name to delete was not given")
}
}