diff --git a/_assets/i18n.json b/_assets/i18n.json index 05e6d6ae..9cc0e3d6 100644 --- a/_assets/i18n.json +++ b/_assets/i18n.json @@ -59,7 +59,8 @@ "errors": { "general": "Unexpected error: `%s`", "no-embed": "Please give me the `Embed Links` permission in this channel. <:googlenerd:317030369205682186>", - "generic-nomessage": "Something went terribly wrong. " + "generic-nomessage": "Something went terribly wrong. ", + "useruploads-disabled": "You are not allowed to upload files.\nContact a Robyul Moderator to find out why: ." }, "permissions": { "required": "Please give me the `%s` permission to use this feature. <:googlenerd:317030369205682186>" @@ -674,6 +675,9 @@ }, "spoiler": { "error-generic": "I'm sorry, I wasn't able to create the spoiler. Please try it again later. " + }, + "useruploads": { + "disable-success": "I disabled useruploads for this user." } } } diff --git a/helpers/useruploads.go b/helpers/useruploads.go new file mode 100644 index 00000000..51700462 --- /dev/null +++ b/helpers/useruploads.go @@ -0,0 +1,35 @@ +package helpers + +import ( + "time" + + "github.com/Seklfreak/Robyul2/models" + "github.com/globalsign/mgo" + "github.com/globalsign/mgo/bson" +) + +func UseruploadsDisableUser(userID, authorID string) (err error) { + return MDbUpsert( + models.UseruploadsDisabledUsersTable, + bson.M{"userid": userID}, + models.UseruploadsDisabledUsersEntry{ + UserID: userID, + BannedByUserID: authorID, + BannedAt: time.Now(), + }, + ) +} + +func UseruploadsIsDisabled(userID string) (disabled bool) { + var thisDisabledUser models.UseruploadsDisabledUsersEntry + err := MdbOne( + MdbCollection(models.UseruploadsDisabledUsersTable).Find(bson.M{"userid": userID}), + &thisDisabledUser, + ) + + if err == mgo.ErrNotFound { + return false + } + + return true +} diff --git a/models/useruploads_disabled_users.go b/models/useruploads_disabled_users.go new file mode 100644 index 00000000..6bddc07c --- /dev/null +++ b/models/useruploads_disabled_users.go @@ -0,0 +1,18 @@ +package models + +import ( + "time" + + "github.com/globalsign/mgo/bson" +) + +const ( + UseruploadsDisabledUsersTable MongoDbCollection = "useruploads_disabled_users" +) + +type UseruploadsDisabledUsersEntry struct { + ID bson.ObjectId `bson:"_id,omitempty"` + UserID string + BannedByUserID string + BannedAt time.Time +} diff --git a/modules/modules.go b/modules/modules.go index fc49430e..b4b63539 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -60,6 +60,7 @@ var ( &plugins.Feedback{}, &plugins.DM{}, &plugins.EmbedPost{}, + &plugins.Useruploads{}, } PluginExtendedList = []ExtendedPlugin{ diff --git a/modules/plugins/levels.go b/modules/plugins/levels.go index d4491193..64c08572 100644 --- a/modules/plugins/levels.go +++ b/modules/plugins/levels.go @@ -539,6 +539,13 @@ func (m *Levels) Action(command string, content string, msg *discordgo.Message, quitChannel := helpers.StartTypingLoop(msg.ChannelID) defer func() { quitChannel <- 0 }() + if helpers.UseruploadsIsDisabled(msg.Author.ID) { + quitChannel <- 0 + _, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.errors.useruploads-disabled")) + helpers.RelaxMessage(err, msg.ChannelID, msg.ID) + return + } + // <= 2 MB, 400x300px? if msg.Attachments[0].Size > 2e+6 || msg.Attachments[0].Width != 400 || msg.Attachments[0].Height != 300 { quitChannel <- 0 @@ -683,6 +690,12 @@ func (m *Levels) Action(command string, content string, msg *discordgo.Message, return } + if helpers.UseruploadsIsDisabled(msg.Author.ID) { + _, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.errors.useruploads-disabled")) + helpers.RelaxMessage(err, msg.ChannelID, msg.ID) + return + } + picData, err := helpers.NetGetUAWithError(backgroundUrl, helpers.DEFAULT_UA) if err != nil { if _, ok := err.(*url.Error); ok { @@ -791,6 +804,12 @@ func (m *Levels) Action(command string, content string, msg *discordgo.Message, return } + if helpers.UseruploadsIsDisabled(msg.Author.ID) { + _, err = helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.errors.useruploads-disabled")) + helpers.RelaxMessage(err, msg.ChannelID, msg.ID) + return + } + channel, err := helpers.GetChannel(msg.ChannelID) helpers.Relax(err) @@ -4563,13 +4582,22 @@ func (m *Levels) logUserBackgroundSet(targetChannelID, sourceChannelID, userID, Name: author.Username + "#" + author.Discriminator + " (#" + author.ID + ")", IconURL: author.AvatarURL("64"), }, - Fields: []*discordgo.MessageEmbedField{{ - Name: "Reset background:", - Value: fmt.Sprintf("`%sprofile background reset %s`", - helpers.GetPrefixForServer(guild.ID), - author.ID), - Inline: false, - }}, + Fields: []*discordgo.MessageEmbedField{ + { + Name: "Reset background:", + Value: fmt.Sprintf("`%sprofile background reset %s`", + helpers.GetPrefixForServer(guild.ID), + author.ID), + Inline: false, + }, + { + Name: "Disable uploads for this user:", + Value: fmt.Sprintf("`%suseruploads disable %s`", + helpers.GetPrefixForServer(guild.ID), + author.ID), + Inline: false, + }, + }, }) return nil diff --git a/modules/plugins/useruploads.go b/modules/plugins/useruploads.go new file mode 100644 index 00000000..cef04d96 --- /dev/null +++ b/modules/plugins/useruploads.go @@ -0,0 +1,96 @@ +package plugins + +import ( + "strings" + + "github.com/Seklfreak/Robyul2/cache" + "github.com/Seklfreak/Robyul2/helpers" + "github.com/bwmarrin/discordgo" + "github.com/sirupsen/logrus" +) + +type useruploadsAction func(args []string, in *discordgo.Message, out **discordgo.MessageSend) (next useruploadsAction) + +type Useruploads struct{} + +func (m *Useruploads) Commands() []string { + return []string{ + "useruploads", + } +} + +func (m *Useruploads) Init(session *discordgo.Session) { +} + +func (m *Useruploads) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) { + defer helpers.Recover() + + session.ChannelTyping(msg.ChannelID) + + var result *discordgo.MessageSend + args := strings.Fields(content) + + action := m.actionStart + for action != nil { + action = action(args, msg, &result) + } +} + +func (m *Useruploads) actionStart(args []string, in *discordgo.Message, out **discordgo.MessageSend) useruploadsAction { + cache.GetSession().ChannelTyping(in.ChannelID) + + if len(args) < 1 { + *out = m.newMsg(helpers.GetText("bot.arguments.too-few")) + return m.actionFinish + } + + switch args[0] { + case "disable": + return m.actionDisable + } + + *out = m.newMsg(helpers.GetText("bot.arguments.invalid")) + return m.actionFinish +} + +func (m *Useruploads) actionDisable(args []string, in *discordgo.Message, out **discordgo.MessageSend) useruploadsAction { + if !helpers.IsRobyulMod(in.Author.ID) { + *out = m.newMsg(helpers.GetText("robyulmod.no_permission")) + return m.actionFinish + } + + if len(args) < 2 { + *out = m.newMsg(helpers.GetText("bot.arguments.too-few")) + return m.actionFinish + } + + targetUser, err := helpers.GetUserFromMention(args[1]) + helpers.Relax(err) + + err = helpers.UseruploadsDisableUser(targetUser.ID, in.Author.ID) + helpers.Relax(err) + + *out = m.newMsg(helpers.GetTextF("plugins.useruploads.disable-success")) + return m.actionFinish +} + +func (m *Useruploads) actionFinish(args []string, in *discordgo.Message, out **discordgo.MessageSend) useruploadsAction { + _, err := helpers.SendComplex(in.ChannelID, *out) + helpers.Relax(err) + + return nil +} + +func (m *Useruploads) newMsg(content string) *discordgo.MessageSend { + return &discordgo.MessageSend{Content: helpers.GetText(content)} +} + +func (m *Useruploads) Relax(err error) { + if err != nil { + panic(err) + } +} + +func (m *Useruploads) logger() *logrus.Entry { + return cache.GetLogger().WithField("module", "Useruploads") +}