Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

23 changed files with 143 additions and 512 deletions

@ -1,40 +0,0 @@
package db
func GetActiveLevel(discordID string) (int, error) {
rows, err := db.Query("SELECT level FROM activeLevel WHERE id=?", discordID)
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var activeLevel int
err := rows.Scan(&activeLevel)
if err != nil {
return 0, err
}
return activeLevel, nil
}
return 0, ErrNotFound
}
func GetTotalActiveLevel() (int, error) {
rows, err := db.Query("SELECT sum(level) FROM activeLevel")
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var totalActiveLevel int
err := rows.Scan(&totalActiveLevel)
if err != nil {
return 0, err
}
return totalActiveLevel, nil
}
return 0, ErrNotFound
}
func AddActiveLevel(discordID string, addedLevel int) error {
_, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET level=level+?", discordID, addedLevel, addedLevel)
return err
}

@ -39,8 +39,4 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
_, err = db.Exec("CREATE TABLE IF NOT EXISTS activeLevel (id TEXT, level INTEGER, UNIQUE(id))")
if err != nil {
panic(err)
}
} }

@ -104,7 +104,7 @@ func GetVoteType(voteID int) (string, error) {
func UpdateVote(voteID int, userID string, voteValue int) error { func UpdateVote(voteID int, userID string, voteValue int) error {
if voteValue == ForceRejectionVote { if voteValue == ForceRejectionVote {
//Check if they used it within a month. //Check if they used it within a month.
rows, err := db.Query("SELECT voteId FROM choices WHERE date >= ? AND value=?", time.Now().AddDate(0, -1, 0), ForceRejectionVote) rows, err := db.Query("SELECT voteId FROM choices WHERE date >= ? AND value=?", time.Now().AddDate(0, -1, 0))
if err != nil { if err != nil {
return err return err
} }

@ -6,35 +6,30 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config" "TerraOceanBot/discord/config"
) )
var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found") var ErrMemberNotFound = errors.New("discord/backend: the requested member was not found")
func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) { func GetAllMembers(s *discordgo.Session) ([]*discordgo.Member, error) {
err := s.RequestGuildMembers(config.GuildID, "", 0) initial, err := s.GuildMembers(config.GuildID, "", 1000)
if err != nil { if err != nil {
return nil, err return nil, err
} }
guild, err := s.State.Guild(config.GuildID) next := initial[len(initial)-1].User.ID
if err != nil { for next != "" {
guild, err = s.Guild(config.GuildID) nextArray, err := s.GuildMembers(config.GuildID, next, 1000)
} if err != nil {
if err != nil { return nil, err
return nil, err }
} if len(nextArray) == 0 {
//TODO Deduplication hack until discordgo fixes this. next = ""
deduplicationCache := make(map[string]bool) } else {
dedupedArray := make([]*discordgo.Member, 0) next = nextArray[len(nextArray)-1].User.ID
for _, v := range guild.Members { initial = append(initial, nextArray...)
if deduplicationCache[v.User.ID] {
continue
} }
dedupedArray = append(dedupedArray, v)
deduplicationCache[v.User.ID] = true
} }
return dedupedArray, nil return initial, nil
} }
func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) { func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordgo.Member, error) {
@ -76,17 +71,5 @@ func GetMemberFromUserFriendlyName(s *discordgo.Session, user string) (*discordg
return v, nil return v, nil
} }
} }
for _, v := range allMembers {
mcUsername, err := db.GetMinecraftUsername(v.User.ID)
if err == db.ErrNotFound {
continue
}
if err != nil {
return nil, err
}
if mcUsername == user {
return v, nil
}
}
return nil, ErrMemberNotFound return nil, ErrMemberNotFound
} }

@ -9,25 +9,28 @@ import (
"TerraOceanBot/server" "TerraOceanBot/server"
) )
func UpdateVoiceChannelState(s *discordgo.Session, guild *discordgo.GuildCreate) { func UpdateVoiceChannelState(s *discordgo.Session) {
if guild.ID != config.GuildID { for _, guild := range s.State.Guilds {
return if guild.ID != config.GuildID {
}
for _, vs := range guild.VoiceStates {
if vs.ChannelID != config.VoiceChannel {
continue
}
if vs.Deaf || vs.SelfDeaf {
continue continue
} }
username, err := db.GetMinecraftUsername(vs.UserID) for _, vs := range guild.VoiceStates {
if err == db.ErrNotFound { if vs.ChannelID != config.VoiceChannel {
continue continue
} }
if err != nil { if vs.Deaf || vs.SelfDeaf {
continue continue
}
username, err := db.GetMinecraftUsername(vs.UserID)
if err == db.ErrNotFound {
continue
}
if err != nil {
message.AuditError(s, "", err)
continue
}
server.ConnectUser(username)
} }
server.ConnectUser(username)
} }
} }

@ -9,35 +9,11 @@ import (
"TerraOceanBot/discord/config" "TerraOceanBot/discord/config"
) )
var trustCache = make(map[string]float64) var trustCache map[string]float64
var ErrNotMember = errors.New("trust: only members have trust value") var ErrNotMember = errors.New("trust: only members have trust value")
func GetTrust(s *discordgo.Session, discordID string) (float64, error) { func GetTrust(s *discordgo.Session, discordID string) (float64, error) {
rawScore, err := GetRawTrust(s, discordID)
if err != nil {
return 0.0, err
}
//Calculate active level
totalLevel, err := db.GetTotalActiveLevel()
if err != nil {
return rawScore, nil
}
if totalLevel < 1000000 {
return rawScore, nil
}
activeLevel, err := db.GetActiveLevel(discordID)
if err != nil && err != db.ErrNotFound {
//ErrNotFound is allowed. It just means the player hasn't played yet.
return 0.0, err
}
activeBuff := float64(activeLevel) / float64(totalLevel)
return rawScore * (1 + activeBuff), nil
}
func GetRawTrust(s *discordgo.Session, discordID string) (float64, error) {
if v, ok := trustCache[discordID]; ok { if v, ok := trustCache[discordID]; ok {
return v, nil return v, nil
} }
@ -58,17 +34,12 @@ func GetRawTrust(s *discordgo.Session, discordID string) (float64, error) {
if err != nil { if err != nil {
return 0.0, err return 0.0, err
} }
if len(votes) == 0 { votes = append(votes, 3)
return 0.0, nil
}
var total float64 var total float64
for _, v := range votes { for _, v := range votes {
total += float64(v) total += float64(v)
} }
return total / float64(len(votes)), nil
rawScore := total / float64(len(votes))
trustCache[discordID] = rawScore
return rawScore, nil
} }
func GetTotalTrust(s *discordgo.Session) (float64, error) { func GetTotalTrust(s *discordgo.Session) (float64, error) {

@ -1,10 +1,5 @@
package config package config
//==Help Command==
const HelpTitle = "指令列表 Command List"
const HelpColour = 0x00E58D
const HelpDescription = "这个信息包含了所有指令的资料。\nThis message contains all the information about available commands."
//==Message Util== //==Message Util==
const UsageTitle = "使用方法 Command Usage" const UsageTitle = "使用方法 Command Usage"
const UsageColour = 0xF00000 const UsageColour = 0xF00000
@ -25,7 +20,7 @@ const InternalErrorDescription = "发生了不明错误。An unknown error has o
//==Vote== //==Vote==
const VoteErrorTitle = "投票错误 Vote Error" const VoteErrorTitle = "投票错误 Vote Error"
const ErrorForceRejectionReuse = "你在这个月内已使用过:x:。请选择其他选项。\nYou have already used :x: this month. Please choose another option." const ErrorForceRejectionReuse = "你在这个月内已使用过:x:。请选择其他选项。\nYou have already used :x: this month. Please choose another option."
const ErrorVoteHasEnded = "投票已结束。无法投票。\nThis vote has already concluded." const ErrorVoteHasEnded = "无法投票给已经结束的投票。\nThis vote has already concluded."
const VoteSuccessfulTitle = "投票成功 Voted Successfully" const VoteSuccessfulTitle = "投票成功 Voted Successfully"
const VoteSuccessfulVoteID = "投票编号 Vote ID" const VoteSuccessfulVoteID = "投票编号 Vote ID"
const VoteSuccessfulVoteName = "投票名称 Vote Name" const VoteSuccessfulVoteName = "投票名称 Vote Name"
@ -46,7 +41,6 @@ const EntryVoteColour = 0x808000
const EntryVoteVoteID = "投票编号 Vote ID" const EntryVoteVoteID = "投票编号 Vote ID"
const EntryVoteApplicant = "申请者 Applicant" const EntryVoteApplicant = "申请者 Applicant"
const EntryVoteReason = "加入原因 Provided Reason for Entry" const EntryVoteReason = "加入原因 Provided Reason for Entry"
const EntryVoteName = "<@%s>的加入 <@%s>'s Entry Application"
const VotePassTitle = "投票通过 Vote Passed" const VotePassTitle = "投票通过 Vote Passed"
const VotePassColour = 0x00F000 const VotePassColour = 0x00F000
@ -54,7 +48,7 @@ const VoteRejectTitle = "投票不通过 Vote Rejected"
const VoteRejectColour = 0xF00000 const VoteRejectColour = 0xF00000
const EntryVoteRejectNotificationTitle = "抱歉…… Sorry…" const EntryVoteRejectNotificationTitle = "抱歉…… Sorry…"
const EntryVoteRejectNotificationContent = "你加入的提案被拒绝了。请提升你和伺服器中的成员的关系后再次申请加入。\nThe application entry you sent was rejected. You must strengthen your relationship with other server members before trying again." const EntryVoteRejectNotificationContent = "你加入的提案最终被拒绝了。请提升你和伺服器中的成员的关系后再次申请加入。\nThe application entry you sent was rejected. You must strengthen your relationship with other server members before trying again."
const EntryVoteSuccessWelcomeTitle = "欢迎! Welcome!" const EntryVoteSuccessWelcomeTitle = "欢迎! Welcome!"
const EntryVoteSuccessWelcomeDescription = "欢迎进入伺服器,<@%s>。Welcome to the server, <@%s>!" const EntryVoteSuccessWelcomeDescription = "欢迎进入伺服器,<@%s>。Welcome to the server, <@%s>!"
@ -67,7 +61,6 @@ const EditAsUsage = "!editas <频道ID> <信息ID> <信息>\n!editas <channel ID
const EditAsSuccessfulDescription = "成功编辑该信息。\nMessage edited successfully." const EditAsSuccessfulDescription = "成功编辑该信息。\nMessage edited successfully."
//VTL is view trust level //VTL is view trust level
const VTLUsage = "!peektrust [目标]\n!peektrust [target]"
const VTLColour = 0xE0E000 const VTLColour = 0xE0E000
const VTLSingleUserTitle = "<@%s>的信誉分 <@%s>'s Trust Score" const VTLSingleUserTitle = "<@%s>的信誉分 <@%s>'s Trust Score"
const VTLSingleUserValue = "分数 Trust Score" const VTLSingleUserValue = "分数 Trust Score"
@ -75,16 +68,13 @@ const VTLSingleUserServerTotal = "伺服器总分 Server Total"
const VTLSingleUserPercentage = "百分比 Percentage" const VTLSingleUserPercentage = "百分比 Percentage"
const VTLGlobalTitle = "伺服器信誉分报告 Server Trust Score Report" const VTLGlobalTitle = "伺服器信誉分报告 Server Trust Score Report"
const VTLGlobalFieldFormat = "%.1f/%.1f (%.2f%%)" const VTLGlobalFieldFormat = "%.1f/%.1f (%.2f%%)"
const ErrorVTLTitle = "生成信誉分报告错误 Server Trust Score Report Generation Error"
const ErrorVTLTargetNotFound = "无法寻找到改变目标。\nCannot find the target user."
const InviteUsage = "!invite <要制造的验证码>\n!invite <validation code to create>" const InviteUsage = "!invite <要制造的验证码>\n!invite <validation code to create>"
const InviteSuccessTitle = "制造验证码成功 Validation Code Creation Success" const InviteSuccessTitle = "制造验证码成功 Validation Code Creation Success"
const InviteSuccessNew = "这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you. Pass it on to a user you want to invite." const InviteSuccessNew = "这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you. Pass it on to a user you want to invite."
const InviteSuccessAlreadyOwn = "该验证码原本就已经属于你。请直接交给你要邀请的人吧。\nThe validation code already belongs to you. You can pass it on to a user you want to invite." const InviteSuccessAlreadyOwn = "该验证码原本就已经属于你。请直接交给你要邀请的人吧。\nThe validation code already belongs to you. You can pass it on to a user you want to invite."
const ErrorInviteTitle = "制造验证码失败 Create Validation Code Error" const ErrorInviteTitle = "制造验证码错误 Create Validation Code Error"
const ErrorInviteCodeContainsSpace = "所制造的验证码不得有任何空格。\nThe validation code to create cannot contain any spaces." const ErrorInviteCodeContainsSpace = "所制造的验证码不得有任何空格。\nThe validation code to create cannot contain any spaces."
const ErrorInviteCodeTooLong = "所制造的验证码必须少于512个字符。\nThe validation code cannot be longer than 512 characters."
const ErrorInviteOwnedByOthers = "这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one." const ErrorInviteOwnedByOthers = "这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one."
const ErrorInviteExistAndUsed = "这个验证码已被使用过。请使用别的验证码。\nThis validation code has already been used. Please try another one." const ErrorInviteExistAndUsed = "这个验证码已被使用过。请使用别的验证码。\nThis validation code has already been used. Please try another one."
@ -98,10 +88,10 @@ const ValidateConfirmationRejectCreatorTitle = "已拒绝验证码使用 Validat
const ValidateConfirmationRejectCreatorDescription = "你已成功拒绝该验证码的使用。为了避免该验证码被对方重复使用,该验证码已被无效化。\nYou have denied the usage of the validation code. To prevent the user from reusing this code, the validation code has been revoked." const ValidateConfirmationRejectCreatorDescription = "你已成功拒绝该验证码的使用。为了避免该验证码被对方重复使用,该验证码已被无效化。\nYou have denied the usage of the validation code. To prevent the user from reusing this code, the validation code has been revoked."
const ValidateConfirmationRejectRejecteeTitle = "验证码使用已被拒绝 Validation Code Usage Denied" const ValidateConfirmationRejectRejecteeTitle = "验证码使用已被拒绝 Validation Code Usage Denied"
const ValidateConfirmationRejectRejecteeDescription = "验证码制造者已拒绝你对该验证码的使用。验证码也已经被无效化了。\nThe validation code creator has prevented you from using the code." const ValidateConfirmationRejectRejecteeDescription = "验证码制造者已拒绝你对该验证码的使用。验证码也已经被无效化了。\nThe validation code creator has prevented you from using the code."
const ValidateConfirmationAcceptCreatorTitle = "已同意验证码使用 Validation Code Usage Approved" const ValidateConfirmationAcceptCreatorTitle = "已同意验证码用 Validation Code Usage Approved"
const ValidateConfirmationAcceptCreatorDescription = "你已成功同意验证码的使用,主伺服器已开始了加入投票。该验证码将在这次使用后无效化。\nYou have successfully approved the usage of the validation code and started a vote in the main server. With the one-time usage property of the code, it has been revoked." const ValidateConfirmationAcceptCreatorDescription = "你已成功同意验证码的使用,主伺服器已开始了加入投票。该验证码也在这次使用后无效化了。\nYou have successfully approved the usage of the validation code and started a vote in the main server. With the one-time usage property of the code, it has been revoked."
const ValidateConfirmationAcceptAccepteeTitle = "验证码使用已被同意 Validation Code Usage Approved" const ValidateConfirmationAcceptAccepteeTitle = "验证码使用已被同意 Validation Code Usage Approved"
const ValidateConfirmationAcceptAccepteeDescription = "你验证码的使用已被同意,主伺服器开始了对于你的加入的投票,请等候投票完毕。\nYour usage of validation code was approved and a vote about your entry request has been started in the main server. Please wait for this process to finish." const ValidateConfirmationAcceptAccepteeDescription = "你验证码的使用已被同意,主伺服器开始了对于你的加入的投票,请等候投票完毕。\nYour usage of validation code was approved and a vote about your entry request has been started in the main server. Please wait for this process to finish."
const ValidateSuccess = "已向验证码制造者发送了请求。\nA request was sent to use the validation code to the creator." const ValidateSuccess = "已向验证码制造者发送了请求。\nA request was sent to use the validation code to the creator."
const ErrorValidateTitle = "验证错误 Validation Error" const ErrorValidateTitle = "验证错误 Validation Error"
const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has already been used. Please request a new one from its creator." const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has already been used. Please request a new one from its creator."
@ -120,7 +110,7 @@ const SetMCUsernameConfirmNewUsername = "新用户名 New Username"
const SetMCUsernameConfirmDeniedTitle = "成功取消设置用户名 Cancelled Set MC Username" const SetMCUsernameConfirmDeniedTitle = "成功取消设置用户名 Cancelled Set MC Username"
const SetMCUsernameConfirmDeniedDescription = "你的用户名没有被改变。请继续使用旧用户名。\nYour username was not changed." const SetMCUsernameConfirmDeniedDescription = "你的用户名没有被改变。请继续使用旧用户名。\nYour username was not changed."
const SetMCUsernameErrorTitle = "设定MC用户名错误 Set MC Username Error" const SetMCUsernameErrorTitle = "设定MC用户名错误 Set MC Username Error"
const ErrorSetMCUsernameDuplicate = "你要设置的用户名已经是你目前记录的用户名。\nThe username you want to set is equal to a username in the record." const ErrorSetMCUsernameDuplicate = "你要设置的用户名已经是你目前记录的用户名。\nThe username you want to set is equal to a username in the record."
const ErrorSetMCUsernameAlreadyOwned = "你要设置的用户名已被其他人使用了。\nThe username you requested is already being used by another player." const ErrorSetMCUsernameAlreadyOwned = "你要设置的用户名已被其他人使用了。\nThe username you requested is already being used by another player."
const ChangeTrustUsage = "!trust <更改对象>\n!trust <target>" const ChangeTrustUsage = "!trust <更改对象>\n!trust <target>"
@ -129,19 +119,14 @@ const ChangeTrustPromptColour = 0xF0F000
const ChangeTrustPromptID = "Discord编号 Discord ID" const ChangeTrustPromptID = "Discord编号 Discord ID"
const ChangeTrustPromptNick = "昵称 Nickname" const ChangeTrustPromptNick = "昵称 Nickname"
const ChangeTrustPromptReminderTitle = "提醒 Reminder" const ChangeTrustPromptReminderTitle = "提醒 Reminder"
const ChangeTrustPromptReminderDescription = "你一个月只能更换对一个玩家的评分进行一次更改。\nYou can only change your trust towards a player once per month." const ChangeTrustPromptReminderDescription = "你一个月只能更换对一个玩家的评分一次。\nYou can only change your trust towards a player once per month."
const ChangeTrustSuccessTitle = "成功更新玩家评分 Update Player Score Success" const ChangeTrustSuccessTitle = "成功更新玩家评分 Update Player Score Success"
const ChangeTrustSuccessDescription = "你对这名玩家的分数改变已被记录。\nThe score has been updated." const ChangeTrustSuccessDescription = "已改变你给这名玩家的分数已被记录。\nThe score has been updated."
const ErrorChangeTrustTitle = "调整信誉分错误 Change Trust Error" const ErrorChangeTrustTitle = "调整信誉分错误 Change Trust Error"
const ErrorChangeTrustTargetNotFound = "无法寻找到改变目标。\nCannot find the target user." const ErrorChangeTrustTargetNotFound = "无法寻找到改变目标。\nCannot find the target user."
const ErrorChangeTrustSelf = "无法改变自己的分数。\nCannot set your own score." const ErrorChangeTrustRecentlyChanged = "你在一个月内有设定过该名玩家的分数。你这次的更动没有被记录。\nYou have already changed your score for this player within the previous month. Your change was not recorded."
const ErrorChangeTrustNotMember = "目标用户不是伺服器成员。\nTarget user is not a server member."
const ErrorChangeTrustRecentlyChanged = "你在一个月内设定过该名玩家的分数。你这次的更动没有被记录。\nYou have already changed your score for this player within the previous month. Your change was not recorded."
const VoteSuggestionUsage = "!votesuggest <种类custom> <内容>\n!votesuggest <type: custom> <content>" const VoteSuggestionUsage = "!votesuggest <种类custom> <内容>\n!votesuggest <type: custom> <content>"
const VoteSuggestionUpcomingTitle = "准备中…… Preparing…" const VoteSuggestionUpcomingTitle = "准备中…… Preparing…"
const VoteSuggestionUpcomingDescription = "正在准备新的投票……\nPreparing for the next vote..." const VoteSuggestionUpcomingDescription = "正在准备新的一个投票……\nPreparing for the next vote..."
const VoteSuggestionUpcomingColour = 0x000000 const VoteSuggestionUpcomingColour = 0x000000
const BatchRoleUsage = "!batchrole <Discord编号>\n!batchrole <Discord ID>"
const BatchRoleSuccessMessage = "成功分发角色。 Distributed role successfully."

@ -17,20 +17,18 @@ func StartBot(token string, kill chan bool) {
} }
dg.AddHandler(ProcessCommand) dg.AddHandler(ProcessCommand)
dg.AddHandler(ProcessVoiceStateUpdate)
dg.AddHandler(modules.CheckForReact)
dg.AddHandler(modules.CheckForVote) dg.AddHandler(modules.CheckForVote)
dg.AddHandler(backend.UpdateVoiceChannelState) dg.AddHandler(modules.CheckForTrustUpdate)
dg.State.TrackMembers = true dg.AddHandler(modules.CheckForInvite)
dg.State.TrackVoice = true dg.AddHandler(modules.ConfirmMinecraftUsername)
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
panic(err) panic(err)
} }
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
modules.GenerateHelpEmbed()
go modules.ListenToVoteFinishes(dg) go modules.ListenToVoteFinishes(dg)
go backend.UpdateVoiceChannelState(dg)
<-kill <-kill

@ -1,9 +1,6 @@
package modules package modules
import ( import (
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"fmt" "fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -53,17 +50,8 @@ var editAs = adminOnly(enforceArgumentCount(config.EditAsUsage, 4,
)) ))
var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) > 1 && command[1] != "unobf" { if len(command) > 1 {
user, err := backend.GetMemberFromUserFriendlyName(s, command[1]) value, err := backend.GetTrust(s, command[1])
if err != nil {
message.InitNewEmbed(
config.ErrorVTLTitle,
config.ErrorVTLTargetNotFound,
config.ErrorColour,
).Send(s, m.ChannelID)
return
}
value, err := backend.GetTrust(s, user.User.ID)
if err != nil { if err != nil {
message.AuditError(s, m.ChannelID, err) message.AuditError(s, m.ChannelID, err)
return return
@ -93,12 +81,7 @@ var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCr
return return
} }
salt := make([]byte, 16)
rand.Read(salt)
//Generate message //Generate message
hash := sha512.New()
hash.Write(salt)
msg := message.NewEmbed().SetColour(config.VTLColour).SetTitle(config.VTLGlobalTitle) msg := message.NewEmbed().SetColour(config.VTLColour).SetTitle(config.VTLGlobalTitle)
for _, v := range members { for _, v := range members {
value, err := backend.GetTrust(s, v.User.ID) value, err := backend.GetTrust(s, v.User.ID)
@ -109,48 +92,10 @@ var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCr
message.AuditError(s, m.ChannelID, err) message.AuditError(s, m.ChannelID, err)
return return
} }
var username string
if len(command) > 1 {
username = v.User.Username + "#" + v.User.Discriminator
} else {
toSalt := []byte(v.User.Username + "#" + v.User.Discriminator)
username = hex.EncodeToString(hash.Sum(toSalt))[:64]
if username == "" {
username = "<cannot calculate hash>"
}
}
msg.AddInlineField( msg.AddInlineField(
username, v.User.Username+"#"+v.User.Discriminator,
fmt.Sprintf(config.VTLGlobalFieldFormat, value, total, value/total*100), fmt.Sprintf(config.VTLGlobalFieldFormat, value, total, value/total*100),
) )
} }
_, err = msg.Send(s, m.ChannelID) msg.Send(s, m.ChannelID)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
}) })
var batchGiveRole = adminOnly(enforceDM(enforceArgumentCount(
config.BatchRoleUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
members, err := backend.GetAllMembers(s)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
for _, member := range members {
err := s.GuildMemberRoleAdd(
config.GuildID,
member.User.ID,
command[1],
)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
}
message.InitNewEmbed(config.SuccessTitle, config.BatchRoleSuccessMessage, config.SuccessColour).
Send(s, m.ChannelID)
},
)))

@ -1,81 +1,44 @@
package modules package modules
import ( import "github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
"github.com/bwmarrin/discordgo"
)
type CommandHandler func(s *discordgo.Session, m *discordgo.MessageCreate, commands []string) type CommandHandler func(s *discordgo.Session, m *discordgo.MessageCreate, commands []string)
type Command struct { type Command struct {
Name string Name string
Handler CommandHandler Handler CommandHandler
Description string
Usage string
Admin bool
} }
var Commands = []Command{ var Commands = []Command{
//Admin Commands
Command{ Command{
Name: "!batchrole", Name: "!sendas",
Handler: batchGiveRole, Handler: sendAs,
Usage: config.BatchRoleUsage,
Admin: true,
}, },
Command{ Command{
Name: "!editas", Name: "!editas",
Handler: editAs, Handler: editAs,
Usage: config.EditAsUsage,
Admin: true,
}, },
Command{ Command{
Name: "!peektrust", Name: "!peektrust",
Handler: viewTrustLevel, Handler: viewTrustLevel,
Usage: config.VTLUsage,
Admin: true,
},
Command{
Name: "!sendas",
Handler: sendAs,
Usage: config.SendAsUsage,
Admin: true,
},
//Regular Commands
Command{
Name: "!help",
Handler: showHelp,
Usage: "!help",
Admin: false,
}, },
Command{ Command{
Name: "!invite", Name: "!invite",
Handler: createInvite, Handler: createInvite,
Usage: config.InviteUsage,
Admin: false,
}, },
Command{ Command{
Name: "!setmcusername", Name: "!validate",
Handler: updateMinecraftUsername, Handler: checkUseInvite,
Usage: config.SetMCUsernameUsage,
Admin: false,
}, },
Command{ Command{
Name: "!trust", Name: "!trust",
Handler: changeTrust, Handler: changeTrust,
Usage: config.ChangeTrustUsage,
Admin: false,
}, },
Command{ Command{
Name: "!validate", Name: "!setmcusername",
Handler: checkUseInvite, Handler: updateMinecraftUsername,
Usage: config.ValidateUsage,
Admin: false,
}, },
Command{ Command{
Name: "!votesuggest", Name: "!votesuggest",
Handler: voteSuggestion, Handler: voteSuggestion,
Usage: config.VoteSuggestionUsage,
Admin: false,
}, },
} }

@ -1,38 +0,0 @@
package modules
import (
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"github.com/bwmarrin/discordgo"
)
var embed *discordgo.MessageEmbed
//GenerateHelpEmbed sets up the embed variable above to be used to display the !help command.
func GenerateHelpEmbed() {
emb := message.NewEmbed().
SetTitle(config.HelpTitle).
SetColour(config.HelpColour).
SetDescription(config.HelpDescription)
for _, cmd := range Commands {
if cmd.Admin {
continue
}
usage := cmd.Usage
if usage == "" {
usage = "No usage provided"
}
emb.AddField(cmd.Name, usage)
}
embed = emb.Build()
}
func showHelp(s *discordgo.Session, m *discordgo.MessageCreate, commands []string) {
_, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
message.AuditError(s, m.ChannelID, err)
}
}

@ -12,6 +12,8 @@ import (
"TerraOceanBot/server" "TerraOceanBot/server"
) )
var pendingInviteConfirmation = make(map[string]invite)
type invite struct { type invite struct {
User string User string
Reason string Reason string
@ -25,11 +27,6 @@ var createInvite = enforceDM(memberFilter(true, enforceArgumentCount(
Send(s, m.ChannelID) Send(s, m.ChannelID)
return return
} }
if len(command[1]) > 512 {
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeTooLong, config.ErrorColour).
Send(s, m.ChannelID)
return
}
err := db.SetInviteOwner(command[1], m.Author.ID) err := db.SetInviteOwner(command[1], m.Author.ID)
switch err { switch err {
@ -65,7 +62,7 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
} }
if err == db.ErrNotFound { if err == db.ErrNotFound {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour). message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour).
Send(s, m.ChannelID) SendPM(s, m.ChannelID)
return return
} }
if err != nil { if err != nil {
@ -88,14 +85,14 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
} }
if !isMember { if !isMember {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour). message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour).
Send(s, m.ChannelID) SendPM(s, m.ChannelID)
return return
} }
channel, err := s.UserChannelCreate(inviter) channel, err := s.UserChannelCreate(inviter)
if err != nil { if err != nil {
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour). message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
Send(s, m.ChannelID) SendPM(s, m.ChannelID)
return return
} }
msg, err := message.InitNewEmbed( msg, err := message.InitNewEmbed(
@ -109,25 +106,30 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
message.AuditErrorPM(s, m.ChannelID, err) message.AuditErrorPM(s, m.ChannelID, err)
return return
} }
queueForReact( pendingInviteConfirmation[msg.ID] = invite{
msg.ID, User: m.Author.ID,
[]string{emojiCheck, emojiX}, Reason: command[2],
invite{ }
User: m.Author.ID,
Reason: command[2],
},
inviteCallback,
)
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck) s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
s.MessageReactionAdd(channel.ID, msg.ID, emojiX) s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour). message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
Send(s, m.ChannelID) SendPM(s, m.ChannelID)
}, },
))) )))
func inviteCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) { func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
invite := info.(invite) if r.UserID == s.State.User.ID {
if r.Emoji.Name == emojiX { return
}
invite, ok := pendingInviteConfirmation[r.MessageID]
if !ok {
return
}
delete(pendingInviteConfirmation, r.MessageID)
switch r.Emoji.Name {
case emojiX:
message.InitNewEmbed( message.InitNewEmbed(
config.ValidateConfirmationRejectCreatorTitle, config.ValidateConfirmationRejectCreatorTitle,
config.ValidateConfirmationRejectCreatorDescription, config.ValidateConfirmationRejectCreatorDescription,
@ -139,24 +141,23 @@ func inviteCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info
config.ErrorColour, config.ErrorColour,
).SendPM(s, invite.User) ).SendPM(s, invite.User)
return return
case emojiCheck:
default:
return
} }
message.InitNewEmbed( message.InitNewEmbed(
config.ValidateConfirmationAcceptCreatorTitle, config.ValidateConfirmationAcceptCreatorTitle,
config.ValidateConfirmationAcceptCreatorDescription, config.ValidateConfirmationAcceptCreatorDescription,
config.SuccessColour, config.ErrorColour,
).SendPM(s, r.UserID) ).SendPM(s, r.UserID)
message.InitNewEmbed( message.InitNewEmbed(
config.ValidateConfirmationAcceptAccepteeTitle, config.ValidateConfirmationAcceptAccepteeTitle,
config.ValidateConfirmationAcceptAccepteeDescription, config.ValidateConfirmationAcceptAccepteeDescription,
config.SuccessColour, config.ErrorColour,
).SendPM(s, invite.User) ).SendPM(s, invite.User)
msg, err := message.InitNewEmbed( msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
config.VoteSuggestionUpcomingTitle,
config.VoteSuggestionUpcomingDescription,
config.VoteSuggestionUpcomingColour,
).Send(s, config.VoteChannel)
if err != nil { if err != nil {
message.AuditErrorPM(s, r.UserID, err) message.AuditErrorPM(s, r.UserID, err)
return return

@ -4,11 +4,14 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"TerraOceanBot/db" "TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config" "TerraOceanBot/discord/config"
"TerraOceanBot/discord/message" "TerraOceanBot/discord/message"
"TerraOceanBot/server" "TerraOceanBot/server"
) )
var minecraftConfirmReact = make(map[string]string)
var updateMinecraftUsername = memberFilter(true, enforceArgumentCount( var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
config.SetMCUsernameUsage, 2, config.SetMCUsernameUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
@ -36,19 +39,23 @@ var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
if err != nil { if err != nil {
message.AuditError(s, m.ChannelID, err) message.AuditError(s, m.ChannelID, err)
} }
queueForReact( minecraftConfirmReact[msg.ID] = command[1]
msg.ID,
[]string{emojiCheck, emojiX},
command[1],
confirmMinecraftUsername,
)
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX) s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX)
}, },
)) ))
func confirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) { func ConfirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
newUsername := info.(string) if r.UserID == s.State.User.ID {
return
}
newUsername, ok := minecraftConfirmReact[r.MessageID]
if !ok {
return
}
delete(minecraftConfirmReact, r.MessageID)
switch r.Emoji.Name { switch r.Emoji.Name {
case emojiCheck: case emojiCheck:
@ -76,13 +83,9 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna
return return
} }
backend.UpdateVoiceChannelState(s)
if oldErr == nil { if oldErr == nil {
server.WhitelistRemove(oldUsername) server.WhitelistRemove(oldUsername)
server.DisconnectUser(oldUsername)
}
if err == nil {
server.WhitelistAdd(newUsername)
server.ConnectUser(newUsername)
} }
message.InitNewEmbed( message.InitNewEmbed(

@ -1,56 +0,0 @@
package modules
import (
"sync"
"github.com/bwmarrin/discordgo"
)
var reactLock sync.Mutex
var reactCallbacks = make(map[string]reactEntry)
type reactCallback func(s *discordgo.Session, react *discordgo.MessageReactionAdd, info interface{})
type reactEntry struct {
acceptedEmoji []string
callback reactCallback
info interface{}
}
func queueForReact(messageID string, acceptedEmoji []string, info interface{}, callback reactCallback) {
reactLock.Lock()
defer reactLock.Unlock()
reactCallbacks[messageID] = reactEntry{
acceptedEmoji: acceptedEmoji,
callback: callback,
info: info,
}
}
func CheckForReact(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
if r.UserID == s.State.User.ID {
return
}
entry, ok := reactCallbacks[r.MessageID]
if !ok {
return
}
reactLock.Lock()
foundEmoji := false
for _, v := range entry.acceptedEmoji {
if r.Emoji.Name == v {
foundEmoji = true
break
}
}
if !foundEmoji {
reactLock.Unlock()
return
}
delete(reactCallbacks, r.MessageID)
reactLock.Unlock()
entry.callback(s, r, entry.info)
}

@ -9,6 +9,8 @@ import (
"TerraOceanBot/discord/message" "TerraOceanBot/discord/message"
) )
var trustMessage = make(map[string]string)
var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount( var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
config.ChangeTrustUsage, 2, config.ChangeTrustUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
@ -26,30 +28,6 @@ var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
return return
} }
if member.User.ID == m.Author.ID {
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustSelf,
config.ErrorColour,
).Send(s, m.ChannelID)
return
}
isMember := false
for _, v := range member.Roles {
if v == config.MemberRoleID {
isMember = true
}
}
if !isMember {
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustNotMember,
config.ErrorColour,
).Send(s, m.ChannelID)
return
}
embed := message.NewEmbed(). embed := message.NewEmbed().
SetTitle(config.ChangeTrustPromptTitle). SetTitle(config.ChangeTrustPromptTitle).
SetColour(config.ChangeTrustPromptColour). SetColour(config.ChangeTrustPromptColour).
@ -66,12 +44,7 @@ var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
return return
} }
queueForReact( trustMessage[msg.ID] = member.User.ID
msg.ID,
[]string{emojiOne, emojiTwo, emojiThree, emojiFour, emojiFive, emojiCheck},
member.User.ID,
trustCallback,
)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiOne) s.MessageReactionAdd(m.ChannelID, msg.ID, emojiOne)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiTwo) s.MessageReactionAdd(m.ChannelID, msg.ID, emojiTwo)
@ -82,8 +55,17 @@ var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
}, },
))) )))
func trustCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) { func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
target := info.(string) if r.UserID == s.State.User.ID {
return
}
target, ok := trustMessage[r.MessageID]
if !ok {
return
}
delete(trustMessage, r.MessageID)
var value int var value int
switch r.Emoji.Name { switch r.Emoji.Name {

@ -7,20 +7,18 @@ import (
) )
type voteType struct { type voteType struct {
//TODO Add FormatName(name string) to clean up vote name
EmbedBuilder func(id int, name string) *message.Embed EmbedBuilder func(id int, name string) *message.Embed
FormatName func(internalName string) string
ResultHandler func(s *discordgo.Session, id int, name string, isPositive bool) ResultHandler func(s *discordgo.Session, id int, name string, isPositive bool)
} }
var voteTypes = map[string]voteType{ var voteTypes = map[string]voteType{
"custom": voteType{ "custom": voteType{
EmbedBuilder: createCustomEmbed, EmbedBuilder: createCustomEmbed,
FormatName: formatCustomName,
ResultHandler: announceCustomResult, ResultHandler: announceCustomResult,
}, },
"invite": voteType{ "invite": voteType{
EmbedBuilder: createInviteEmbed, EmbedBuilder: createInviteEmbed,
FormatName: formatInviteName,
ResultHandler: handleInviteResult, ResultHandler: handleInviteResult,
}, },
} }

@ -15,10 +15,6 @@ func createCustomEmbed(id int, name string) *message.Embed {
AddField(config.CustomVoteContent, name) AddField(config.CustomVoteContent, name)
} }
func formatCustomName(internalName string) string {
return internalName
}
func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) { func announceCustomResult(s *discordgo.Session, id int, name string, isPositive bool) {
createCustomEmbed(id, name). createCustomEmbed(id, name).
UpdateVoteStatus(isPositive). UpdateVoteStatus(isPositive).

@ -21,11 +21,6 @@ func createInviteEmbed(id int, name string) *message.Embed {
AddField(config.EntryVoteReason, list[1]) AddField(config.EntryVoteReason, list[1])
} }
func formatInviteName(internalName string) string {
list := strings.SplitN(internalName, ":", 2)
return fmt.Sprintf(config.EntryVoteName, list[0], list[0])
}
func handleInviteResult(s *discordgo.Session, id int, name string, isPositive bool) { func handleInviteResult(s *discordgo.Session, id int, name string, isPositive bool) {
list := strings.SplitN(name, ":", 2) list := strings.SplitN(name, ":", 2)
if !isPositive { if !isPositive {

@ -71,13 +71,10 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
return return
} }
var voteName, voteType string var voteName string
if err == nil { if err == nil {
voteName, err = db.GetVoteName(voteID) voteName, err = db.GetVoteName(voteID)
} }
if err == nil {
voteType, err = db.GetVoteType(voteID)
}
if err != nil { if err != nil {
message.AuditErrorPM(s, r.UserID, err) message.AuditErrorPM(s, r.UserID, err)
@ -86,7 +83,7 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
message.InitNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour). message.InitNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour).
AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)). AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)).
AddField(config.VoteSuccessfulVoteName, voteTypes[voteType].FormatName(voteName)). AddField(config.VoteSuccessfulVoteName, voteName).
AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name). AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name).
SendPM(s, r.UserID) SendPM(s, r.UserID)
message.AuditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID)) message.AuditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
@ -97,7 +94,7 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
var voteSuggestion = enforceDM(enforceArgumentCount( var voteSuggestion = enforceDM(enforceArgumentCount(
config.VoteSuggestionUsage, 3, config.VoteSuggestionUsage, 3,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command[2]) > 500 { if len(command[1]) > 500 {
message.InitNewEmbed( message.InitNewEmbed(
config.VoteSuggestionErrorTitle, config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionTooLong, config.ErrorVoteSuggestionTooLong,
@ -106,7 +103,7 @@ var voteSuggestion = enforceDM(enforceArgumentCount(
return return
} }
switch command[1] { switch command[0] {
case "custom": case "custom":
msg, err := message.InitNewEmbed( msg, err := message.InitNewEmbed(
config.VoteSuggestionUpcomingTitle, config.VoteSuggestionUpcomingTitle,
@ -117,14 +114,14 @@ var voteSuggestion = enforceDM(enforceArgumentCount(
message.AuditErrorPM(s, m.Author.ID, err) message.AuditErrorPM(s, m.Author.ID, err)
return return
} }
id, err := db.CreateCustomVote(msg.ID, command[2]) id, err := db.CreateCustomVote(msg.ID, command[1])
if err != nil { if err != nil {
message.AuditErrorPM(s, m.Author.ID, err) message.AuditErrorPM(s, m.Author.ID, err)
return return
} }
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID)) message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "") s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
createCustomEmbed(id, command[2]).Edit(s, config.VoteChannel, msg.ID) createCustomEmbed(id, command[1]).Edit(s, config.VoteChannel, msg.ID)
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiOne) s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiOne)
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiTwo) s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiTwo)
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiThree) s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiThree)

@ -18,8 +18,6 @@ type confirmedResult struct {
IsPositive bool IsPositive bool
} }
const invalidVote = -1
var voteMutex sync.Mutex var voteMutex sync.Mutex
var toAnnounceResultList []confirmedResult var toAnnounceResultList []confirmedResult
@ -27,9 +25,6 @@ func ListenToVoteFinishes(s *discordgo.Session) {
for { for {
voteMutex.Lock() voteMutex.Lock()
for _, v := range toAnnounceResultList { for _, v := range toAnnounceResultList {
if v.VoteID == invalidVote {
continue
}
message.AuditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID)) message.AuditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID))
if err := db.FinishVote(v.VoteID); err != nil { if err := db.FinishVote(v.VoteID); err != nil {
message.AuditError(s, "", err) message.AuditError(s, "", err)
@ -77,13 +72,13 @@ func checkForVoteResult(s *discordgo.Session, id int) {
lowestPossible := (currentScore + remainingTrust) / totalGlobalTrust lowestPossible := (currentScore + remainingTrust) / totalGlobalTrust
highestPossible := (currentScore + remainingTrust*5) / totalGlobalTrust highestPossible := (currentScore + remainingTrust*5) / totalGlobalTrust
voteMutex.Lock()
defer voteMutex.Unlock()
if highestPossible <= 3.5 || absoluteRejectionVote { if highestPossible <= 3.5 || absoluteRejectionVote {
//Rejection confirmed. //Rejection confirmed.
voteMutex.Lock()
for _, v := range toAnnounceResultList { for _, v := range toAnnounceResultList {
if v.VoteID == id { if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle. //It's already queued to be announced in the next announcement cycle.
voteMutex.Unlock()
return return
} }
} }
@ -92,11 +87,15 @@ func checkForVoteResult(s *discordgo.Session, id int) {
VoteID: id, VoteID: id,
IsPositive: false, IsPositive: false,
}) })
} else if lowestPossible >= 3.5 { voteMutex.Unlock()
}
if lowestPossible >= 3.5 {
//Acceptance confirmed. //Acceptance confirmed.
voteMutex.Lock()
for _, v := range toAnnounceResultList { for _, v := range toAnnounceResultList {
if v.VoteID == id { if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle. //It's already queued to be announced in the next announcement cycle.
voteMutex.Unlock()
return return
} }
} }
@ -105,17 +104,6 @@ func checkForVoteResult(s *discordgo.Session, id int) {
VoteID: id, VoteID: id,
IsPositive: true, IsPositive: true,
}) })
} else { voteMutex.Unlock()
//Nothing happened. Make sure it's not in the queue.
for k, v := range toAnnounceResultList {
if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle.
toAnnounceResultList[k] = confirmedResult{
VoteID: invalidVote,
IsPositive: false,
}
return
}
}
} }
} }

@ -3,10 +3,8 @@ module TerraOceanBot
go 1.13 go 1.13
require ( require (
github.com/bwmarrin/discordgo v0.20.1 github.com/bwmarrin/discordgo v0.19.0
github.com/gorilla/mux v1.7.3 // indirect github.com/gorilla/mux v1.7.3 // indirect
github.com/gorilla/websocket v1.4.1 github.com/gorilla/websocket v1.4.1
github.com/mattn/go-sqlite3 v1.11.0 github.com/mattn/go-sqlite3 v1.11.0
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20191018095205-727590c5006e // indirect
) )

@ -1,5 +1,5 @@
github.com/bwmarrin/discordgo v0.20.1 h1:Ihh3/mVoRwy3otmaoPDUioILBJq4fdWkpsi83oj2Lmk= github.com/bwmarrin/discordgo v0.19.0 h1:kMED/DB0NR1QhRcalb85w0Cu3Ep2OrGAqZH1R5awQiY=
github.com/bwmarrin/discordgo v0.20.1/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
@ -8,14 +8,5 @@ github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvK
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191018095205-727590c5006e h1:ZtoklVMHQy6BFRHkbG6JzK+S6rX82//Yeok1vMlizfQ=
golang.org/x/sys v0.0.0-20191018095205-727590c5006e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

@ -2,12 +2,9 @@ package server
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"TerraOceanBot/db"
) )
var clients = make(map[*websocket.Conn]bool) var clients = make(map[*websocket.Conn]bool)
@ -30,32 +27,8 @@ func handleIncomingMessage(who *websocket.Conn, msg string) {
return return
} }
if !connectedUsers[message[1]] { if !connectedUsers[message[1]] {
fmt.Println(message[1], "tried to connect. Disconnecting!")
fmt.Println(connectedUsers)
who.WriteMessage(websocket.TextMessage, []byte("leave "+message[1])) who.WriteMessage(websocket.TextMessage, []byte("leave "+message[1]))
} }
case "active":
arg := strings.Split(msg, " ")
if len(arg) < 3 {
return
}
arg[1] = strings.Join(arg[1:len(arg)-1], " ")
arg[2] = arg[len(arg)-1]
id, err := db.GetDiscordID(arg[1])
if err != nil {
fmt.Println(err)
return
}
value, err := strconv.Atoi(arg[2])
if err != nil {
fmt.Println(err)
return
}
err = db.AddActiveLevel(id, value)
if err != nil {
fmt.Println(err)
return
}
default: default:
fmt.Println("Unknown message:" + msg) fmt.Println("Unknown message:" + msg)
} }
@ -73,7 +46,6 @@ func ConnectUser(mcUsername string) {
func DisconnectUser(mcUsername string) { func DisconnectUser(mcUsername string) {
delete(connectedUsers, mcUsername) delete(connectedUsers, mcUsername)
broadcast("leave " + mcUsername)
} }
func RequestSync() { func RequestSync() {