feat: support Discord Guild Join
This commit is contained in:
parent
4b9756b257
commit
fd72565011
@ -56,6 +56,9 @@ var GitHubClientSecret = ""
|
|||||||
|
|
||||||
var DiscordClientId = ""
|
var DiscordClientId = ""
|
||||||
var DiscordClientSecret = ""
|
var DiscordClientSecret = ""
|
||||||
|
var DiscordGuildId = ""
|
||||||
|
var DiscordAllowJoiningGuild = "false"
|
||||||
|
var DiscordBotToken = ""
|
||||||
|
|
||||||
var WeChatServerAddress = ""
|
var WeChatServerAddress = ""
|
||||||
var WeChatServerToken = ""
|
var WeChatServerToken = ""
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"one-api/model"
|
"one-api/model"
|
||||||
@ -36,7 +38,7 @@ func getDiscordUserInfoByCode(codeFromURLParamaters string, host string) (*Disco
|
|||||||
ClientID: common.DiscordClientId,
|
ClientID: common.DiscordClientId,
|
||||||
ClientSecret: common.DiscordClientSecret,
|
ClientSecret: common.DiscordClientSecret,
|
||||||
RedirectURI: fmt.Sprintf("https://%s/oauth/discord", host),
|
RedirectURI: fmt.Sprintf("https://%s/oauth/discord", host),
|
||||||
Scopes: []string{disgoauth.ScopeIdentify, disgoauth.ScopeEmail},
|
Scopes: []string{disgoauth.ScopeIdentify, disgoauth.ScopeEmail, disgoauth.ScopeGuilds, disgoauth.ScopeGuildsJoin},
|
||||||
})
|
})
|
||||||
|
|
||||||
accessToken, _ := dc.GetOnlyAccessToken(codeFromURLParamaters)
|
accessToken, _ := dc.GetOnlyAccessToken(codeFromURLParamaters)
|
||||||
@ -58,6 +60,46 @@ func getDiscordUserInfoByCode(codeFromURLParamaters string, host string) (*Disco
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add guild member.
|
||||||
|
if common.DiscordGuildId != "" && discordUser.Id != "" && common.DiscordBotToken != "" && common.DiscordAllowJoiningGuild == "true" {
|
||||||
|
url := fmt.Sprintf("https://discord.com/api/guilds/%s/members/%s", common.DiscordGuildId, discordUser.Id)
|
||||||
|
|
||||||
|
// Set JSON
|
||||||
|
map1 := map[string]interface{}{
|
||||||
|
// accessToken remove "Bearer "
|
||||||
|
"access_token": string(accessToken[7:]),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert map to JSON
|
||||||
|
jsonData, _ := json.Marshal(map1)
|
||||||
|
|
||||||
|
req, _ := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
|
||||||
|
|
||||||
|
// Set Header
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", common.DiscordBotToken))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// Create a new HTTP Client
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
|
||||||
|
log.Print(resp.StatusCode)
|
||||||
|
|
||||||
|
if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 201) {
|
||||||
|
// Print content
|
||||||
|
stringBuff := new(bytes.Buffer)
|
||||||
|
stringBuff.ReadFrom(resp.Body)
|
||||||
|
|
||||||
|
// Print error
|
||||||
|
fmt.Println("Error: ", stringBuff.String())
|
||||||
|
|
||||||
|
return nil, errors.New("You must join the discord server first or be verified member to be able to login!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the response body
|
||||||
|
defer resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
if discordUser.Username == "" {
|
if discordUser.Username == "" {
|
||||||
return nil, errors.New("Invalid return value, user field is empty, please try again later!")
|
return nil, errors.New("Invalid return value, user field is empty, please try again later!")
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,8 @@ func GetStatus(c *gin.Context) {
|
|||||||
"github_client_id": common.GitHubClientId,
|
"github_client_id": common.GitHubClientId,
|
||||||
"discord_oauth": common.DiscordOAuthEnabled,
|
"discord_oauth": common.DiscordOAuthEnabled,
|
||||||
"discord_client_id": common.DiscordClientId,
|
"discord_client_id": common.DiscordClientId,
|
||||||
|
"discord_guild_id": common.DiscordGuildId,
|
||||||
|
"discord_allow_joining_guild": common.DiscordAllowJoiningGuild,
|
||||||
"system_name": common.SystemName,
|
"system_name": common.SystemName,
|
||||||
"logo": common.Logo,
|
"logo": common.Logo,
|
||||||
"footer_html": common.Footer,
|
"footer_html": common.Footer,
|
||||||
|
@ -56,6 +56,9 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["GitHubClientSecret"] = ""
|
common.OptionMap["GitHubClientSecret"] = ""
|
||||||
common.OptionMap["DiscordClientId"] = ""
|
common.OptionMap["DiscordClientId"] = ""
|
||||||
common.OptionMap["DiscordClientSecret"] = ""
|
common.OptionMap["DiscordClientSecret"] = ""
|
||||||
|
common.OptionMap["DiscordGuildId"] = ""
|
||||||
|
common.OptionMap["DiscordBotToken"] = ""
|
||||||
|
common.OptionMap["DiscordAllowJoiningGuild"] = ""
|
||||||
common.OptionMap["WeChatServerAddress"] = ""
|
common.OptionMap["WeChatServerAddress"] = ""
|
||||||
common.OptionMap["WeChatServerToken"] = ""
|
common.OptionMap["WeChatServerToken"] = ""
|
||||||
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
||||||
@ -178,6 +181,12 @@ func updateOptionMap(key string, value string) (err error) {
|
|||||||
common.GitHubClientSecret = value
|
common.GitHubClientSecret = value
|
||||||
case "DiscordClientId":
|
case "DiscordClientId":
|
||||||
common.DiscordClientId = value
|
common.DiscordClientId = value
|
||||||
|
case "DiscordGuildId":
|
||||||
|
common.DiscordGuildId = value
|
||||||
|
case "DiscordBotToken":
|
||||||
|
common.DiscordBotToken = value
|
||||||
|
case "DiscordAllowJoiningGuild":
|
||||||
|
common.DiscordAllowJoiningGuild = value
|
||||||
case "DiscordClientSecret":
|
case "DiscordClientSecret":
|
||||||
common.DiscordClientSecret = value
|
common.DiscordClientSecret = value
|
||||||
case "Footer":
|
case "Footer":
|
||||||
|
@ -59,7 +59,7 @@ const LoginForm = () => {
|
|||||||
|
|
||||||
const onDiscordOAuthClicked = () => {
|
const onDiscordOAuthClicked = () => {
|
||||||
window.open(
|
window.open(
|
||||||
`https://discord.com/oauth2/authorize?response_type=code&client_id=${status.discord_client_id}&redirect_uri=${window.location.origin}/oauth/discord&scope=identify`,
|
`https://discord.com/oauth2/authorize?response_type=code&client_id=${status.discord_client_id}&redirect_uri=${window.location.origin}/oauth/discord&scope=identify%20guilds%20email%20guilds.join`,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,6 +12,9 @@ const SystemSetting = () => {
|
|||||||
GitHubClientId: '',
|
GitHubClientId: '',
|
||||||
GitHubClientSecret: '',
|
GitHubClientSecret: '',
|
||||||
DiscordClientId: '',
|
DiscordClientId: '',
|
||||||
|
DiscordAllowJoiningGuild: 'false',
|
||||||
|
DiscordGuildId: '',
|
||||||
|
DiscordBotToken: '',
|
||||||
DiscordClientSecret: '',
|
DiscordClientSecret: '',
|
||||||
Notice: '',
|
Notice: '',
|
||||||
SMTPServer: '',
|
SMTPServer: '',
|
||||||
@ -87,6 +90,9 @@ const SystemSetting = () => {
|
|||||||
name.startsWith('SMTP') ||
|
name.startsWith('SMTP') ||
|
||||||
name === 'ServerAddress' ||
|
name === 'ServerAddress' ||
|
||||||
name === 'DiscordClientId' ||
|
name === 'DiscordClientId' ||
|
||||||
|
name === 'DiscordGuildId' ||
|
||||||
|
name === 'DiscordAllowJoiningGuild' ||
|
||||||
|
name === 'DiscordBotToken' ||
|
||||||
name === 'DiscordClientSecret' ||
|
name === 'DiscordClientSecret' ||
|
||||||
name === 'GitHubClientId' ||
|
name === 'GitHubClientId' ||
|
||||||
name === 'GitHubClientSecret' ||
|
name === 'GitHubClientSecret' ||
|
||||||
@ -177,6 +183,24 @@ const SystemSetting = () => {
|
|||||||
) {
|
) {
|
||||||
await updateOption('DiscordClientSecret', inputs.DiscordClientSecret);
|
await updateOption('DiscordClientSecret', inputs.DiscordClientSecret);
|
||||||
}
|
}
|
||||||
|
if (originInputs['DiscordGuildId'] !== inputs.DiscordGuildId) {
|
||||||
|
await updateOption('DiscordGuildId', inputs.DiscordGuildId);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
originInputs['DiscordBotToken'] !== inputs.DiscordBotToken &&
|
||||||
|
inputs.DiscordBotToken !== ''
|
||||||
|
) {
|
||||||
|
await updateOption('DiscordBotToken', inputs.DiscordBotToken);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
originInputs['DiscordAllowJoiningGuild'] !==
|
||||||
|
inputs.DiscordAllowJoiningGuild
|
||||||
|
) {
|
||||||
|
await updateOption(
|
||||||
|
'DiscordAllowJoiningGuild',
|
||||||
|
inputs.DiscordAllowJoiningGuild,
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const submitTurnstile = async () => {
|
const submitTurnstile = async () => {
|
||||||
@ -352,6 +376,32 @@ const SystemSetting = () => {
|
|||||||
value={inputs.DiscordClientSecret}
|
value={inputs.DiscordClientSecret}
|
||||||
placeholder='Sensitive information will not be displayed in the frontend'
|
placeholder='Sensitive information will not be displayed in the frontend'
|
||||||
/>
|
/>
|
||||||
|
<Form.Checkbox
|
||||||
|
label='Allow Joining Guild'
|
||||||
|
name='DiscordAllowJoiningGuild'
|
||||||
|
autoComplete='new-password'
|
||||||
|
checked={inputs.DiscordAllowJoiningGuild === 'true'}
|
||||||
|
onChange={(e, { name, checked }) =>
|
||||||
|
handleInputChange(e, { name, value: checked ? 'true' : 'false' })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<Form.Input
|
||||||
|
label='Discord Guild ID'
|
||||||
|
name='DiscordGuildId'
|
||||||
|
onChange={handleInputChange}
|
||||||
|
autoComplete='new-password'
|
||||||
|
value={inputs.DiscordGuildId}
|
||||||
|
placeholder='Enter the ID of your Discord server'
|
||||||
|
/>
|
||||||
|
<Form.Input
|
||||||
|
label='Discord Bot Token'
|
||||||
|
name='DiscordBotToken'
|
||||||
|
onChange={handleInputChange}
|
||||||
|
type='password'
|
||||||
|
autoComplete='new-password'
|
||||||
|
value={inputs.DiscordBotToken}
|
||||||
|
placeholder='Sensitive information will not be displayed in the frontend'
|
||||||
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Button onClick={submitDiscordOAuth}>
|
<Form.Button onClick={submitDiscordOAuth}>
|
||||||
Save Discord OAuth Settings
|
Save Discord OAuth Settings
|
||||||
|
Loading…
Reference in New Issue
Block a user