Skip to content
This repository has been archived by the owner on Mar 6, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/evieDelta/covebotnt
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieDelta committed Oct 17, 2020
2 parents 80d700f + be40344 commit bf9c29f
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 49 deletions.
21 changes: 10 additions & 11 deletions blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"fmt"
)

func getBlacklistAll() map[uint64][]uint64 {
channelBlacklist := make(map[uint64][]uint64)
var guilds []uint64
func getBlacklistAll() (channelBlacklist map[string][]string) {
var guilds []string

guildRows, err := db.Query(context.Background(), "select distinct server_id from starboard_blacklisted_channels")
if err != nil {
Expand All @@ -16,13 +15,13 @@ func getBlacklistAll() map[uint64][]uint64 {
defer guildRows.Close()

for guildRows.Next() {
var guild uint64
var guild string
err = guildRows.Scan(&guild)
guilds = append(guilds, guild)
}

for _, guild := range guilds {
var channels []uint64
var channels []string

channelRows, err := db.Query(context.Background(), "select channel_id from starboard_blacklisted_channels where server_id=$1", guild)
if err != nil {
Expand All @@ -31,7 +30,7 @@ func getBlacklistAll() map[uint64][]uint64 {
defer channelRows.Close()

for channelRows.Next() {
var channel uint64
var channel string
err = channelRows.Scan(&channel)
if err != nil {
panic(err)
Expand All @@ -44,8 +43,8 @@ func getBlacklistAll() map[uint64][]uint64 {
return channelBlacklist
}

func getBlacklistForGuild(guildID uint64) error {
var channels []uint64
func getBlacklistForGuild(guildID string) error {
var channels []string

channelRows, err := db.Query(context.Background(), "select channel_id from starboard_blacklisted_channels where server_id=$1", guildID)
if err != nil {
Expand All @@ -54,7 +53,7 @@ func getBlacklistForGuild(guildID uint64) error {
defer channelRows.Close()

for channelRows.Next() {
var channel uint64
var channel string
err = channelRows.Scan(&channel)
if err != nil {
return err
Expand All @@ -66,7 +65,7 @@ func getBlacklistForGuild(guildID uint64) error {
return nil
}

func addChannelsToBlacklist(guildID uint64, channels []uint64) error {
func addChannelsToBlacklist(guildID string, channels []string) error {
for _, channel := range channelBlacklist[guildID] {
for i, newChannel := range channels {
if channel == newChannel {
Expand All @@ -92,7 +91,7 @@ func addChannelsToBlacklist(guildID uint64, channels []uint64) error {
return nil
}

func removeFromSlice(s []uint64, i int) []uint64 {
func removeFromSlice(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
1 change: 1 addition & 0 deletions commandErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "github.com/bwmarrin/discordgo"
func commandError(err error, s *discordgo.Session, m *discordgo.MessageCreate) {
s.MessageReactionAdd(m.ChannelID, m.ID, "❌")
s.ChannelMessageSend(m.ChannelID, "❌ An error occured:\n> "+err.Error())
sugar.Errorf("Command error occured: ", err.Error())
}
2 changes: 2 additions & 0 deletions commandTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ func commandTree(command string, args []string, s *discordgo.Session, m *discord
commandHelp(args, s, m)
case "setstatus":
commandSetStatus(args, s, m)
case "starboard":
commandStarboard(args, s, m)
}
}
2 changes: 1 addition & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ bot_admins = ["0", "0", "0"]
prefixes = ["!"]
[auth]
token = 'secretDiscordString'
database_url = 'postgresql://starbot:starbot@localhost/starbot'
database_url = 'postgresql://starbot:starbot@localhost/starbot'
20 changes: 20 additions & 0 deletions configCommands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "github.com/bwmarrin/discordgo"

func commandStarboard(args []string, s *discordgo.Session, m *discordgo.MessageCreate) {
// this command needs the mod role or administrator perms
perms := checkModRole(s, m.Author.ID, m.GuildID, false)
if perms != nil {
commandError(perms, s, m)
return
}
if len(args) == 0 {
s.ChannelMessageSendEmbed(m.ChannelID, currentStarboardSettings(m.GuildID))
}
s.ChannelMessageSend(m.ChannelID, "congrats you have permission to do this")
}

func currentStarboardSettings(guildID string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{}
}
36 changes: 19 additions & 17 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,41 @@ var initDBSql = `create type modaction as enum ('warn', 'mute', 'unmute', 'pause
create table if not exists guild_settings
(
guild_id bigint primary key,
starboard_channel bigint not null default 0,
guild_id text primary key,
prefix text default '',
starboard_channel text not null default '0',
react_limit int not null default 100,
emoji text not null default '⭐',
sender_can_react boolean default false,
react_to_starboard boolean default true,
mod_roles bigint[] not null default array[0],
helper_roles bigint[] not null default array[0],
mod_log bigint not null default 0,
mute_role bigint not null default 0,
pause_role bigint not null default 0,
mod_roles text[] not null default array['0'],
helper_roles text[] not null default array['0'],
mod_log text not null default '0',
mute_role text not null default '0',
pause_role text not null default '0',
gatekeeper_roles bigint[] not null default array[0],
member_roles bigint[] not null default array[0],
gatekeeper_channel bigint not null default 0,
gatekeeper_roles text[] not null default array['0'],
member_roles text[] not null default array['0'],
gatekeeper_channel text not null default '0',
gatekeeper_message text not null default 'Please wait to be approved, {mention}.',
welcome_channel bigint not null default 0,
welcome_channel text not null default '0',
welcome_message text not null default 'Welcome to {guild}, {mention}!'
);
create table if not exists starboard_messages
(
message_id bigint primary key,
channel_id bigint,
server_id bigint not null references guild_settings (guild_id) on delete cascade,
starboard_message_id bigint
message_id text primary key,
channel_id text not null,
server_id text not null references guild_settings (guild_id) on delete cascade,
starboard_message_id text
);
create table if not exists starboard_blacklisted_channels
(
channel_id bigint primary key,
server_id bigint not null references guild_settings (guild_id) on delete cascade
channel_id text primary key,
server_id text not null references guild_settings (guild_id) on delete cascade
);
create table if not exists info
Expand Down
42 changes: 22 additions & 20 deletions guildSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@ import (
)

type guildSettings struct {
Prefix string
Starboard starboardSettings
Moderation modSettings
Gatekeeper gatekeeperSettings
}
type starboardSettings struct {
StarboardID uint64
StarboardID string
ReactLimit int
Emoji string
SenderCanReact bool
ReactToStarboard bool
}
type modSettings struct {
ModRoles []uint64
HelperRoles []uint64
ModLog uint64
MuteRole uint64
PauseRole uint64
ModRoles []string
HelperRoles []string
ModLog string
MuteRole string
PauseRole string
}
type gatekeeperSettings struct {
GatekeeperRoles []uint64
MemberRoles []uint64
GatekeeperChannel uint64
GatekeeperRoles []string
MemberRoles []string
GatekeeperChannel string
GatekeeperMessage string
WelcomeChannel uint64
WelcomeChannel string
WelcomeMessage string
}

func updateSettingsForGuild(guildID uint64) error {
func updateSettingsForGuild(guildID string) error {
return nil
}

func getSettingsAll() (map[uint64]guildSettings, error) {
settings := make(map[uint64]guildSettings)

func getSettingsAll() (settings map[string]guildSettings, err error) {
// get starboard settings
rows, err := db.Query(context.Background(), "select * from guild_settings")
if err != nil {
Expand All @@ -48,25 +47,28 @@ func getSettingsAll() (map[uint64]guildSettings, error) {

for rows.Next() {
var (
guildID, starboardChannel uint64
guildID, prefix string

starboardChannel string
reactLimit int
emoji string
senderCanReact, reactToStarboard bool

modRoles, helperRoles []uint64
modLog, muteRole, pauseRole uint64
modRoles, helperRoles []string
modLog, muteRole, pauseRole string

gatekeeperRoles, memberRoles []uint64
gatekeeperChannel, welcomeChannel uint64
gatekeeperRoles, memberRoles []string
gatekeeperChannel, welcomeChannel string
gatekeeperMessage, welcomeMessage string
)
rows.Scan(
&guildID, &starboardChannel, &reactLimit, &emoji, &senderCanReact, &reactToStarboard,
&guildID, &prefix, &starboardChannel, &reactLimit, &emoji, &senderCanReact, &reactToStarboard,
&modRoles, &helperRoles, &modLog, &muteRole, &pauseRole,
&gatekeeperRoles, &memberRoles, &gatekeeperChannel,
&gatekeeperMessage, &welcomeChannel, &welcomeMessage,
)
settings[guildID] = guildSettings{
Prefix: prefix,
Starboard: starboardSettings{starboardChannel, reactLimit, emoji, senderCanReact, reactToStarboard},
Moderation: modSettings{modRoles, helperRoles, modLog, muteRole, pauseRole},
Gatekeeper: gatekeeperSettings{gatekeeperRoles, memberRoles, gatekeeperChannel,
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func main() {
// add message create handler for commands
dg.AddHandler(messageCreateCommand)

dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentsGuildEmojis | discordgo.IntentsDirectMessages | discordgo.IntentsGuildMessageReactions)

err = dg.Open()
if err != nil {
panic(err)
Expand Down
58 changes: 58 additions & 0 deletions permissionValidators.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "github.com/bwmarrin/discordgo"

func checkOwner(userID string) error {
for _, ownerID := range config.Bot.BotOwners {
if userID == ownerID {
Expand All @@ -8,3 +10,59 @@ func checkOwner(userID string) error {
}
return &errorNoPermissions{"BotOwner"}
}

func checkModRole(s *discordgo.Session, memberID, guildID string, checkHelperRoles bool) error {
// get the guild
guild, err := s.Guild(guildID)
if err != nil {
return err
}

// get the member
member, err := s.GuildMember(guildID, memberID)
if err != nil {
return err
}

// if the user is the guild owner, they have permission to use the command
if member.User.ID == guild.OwnerID {
return nil
}

// check if the user has a mod role
for _, modRole := range globalSettings[guildID].Moderation.ModRoles {
for _, role := range member.Roles {
if role == modRole {
return nil
}
}
}

// if this command only requires a helper role, check those too
if checkHelperRoles {
for _, helperRole := range globalSettings[guildID].Moderation.HelperRoles {
for _, role := range member.Roles {
if role == helperRole {
return nil
}
}
}
}

// if not we check for admin perms
// iterate through all guild roles
for _, r := range guild.Roles {
// iterate through member roles
for _, u := range member.Roles {
// if they have the role...
if u == r.ID {
// ...and the role has admin perms, return
if r.Permissions&discordgo.PermissionAdministrator == discordgo.PermissionAdministrator {
return nil
}
}
}
}

return &errorNoPermissions{"Administrator or ModRole"}
}

0 comments on commit bf9c29f

Please sign in to comment.