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

Commit

Permalink
feat: discord webhook支持下拉选择
Browse files Browse the repository at this point in the history
  • Loading branch information
yefu24324 committed Sep 1, 2021
1 parent 2cd504c commit cb132d7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ export interface DToQUserLimitEntity {
}

export interface DiscordAllGuildAndChannelsInfo {
guild: Array<{
id: string,
name: string
channels: Array<{ id: string, name: string }>
}>;
guild: DiscordGuildInfo[];
}
export interface DiscordGuildInfo {
id: string;
name: string
channels: Array<{ id: string, name: string }>;
hasManageWebhooks: boolean;
webhooks: Array<{ id: string, name: string, token: string }>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
</nb-select>
</div>

<ng-container *ngIf="discordInfo.hasManageWebhooks">
<ng-container *ngIf="getChannelGuild(bridge.discord.channelID)?.hasManageWebhooks">
<div class="form-group">
<label for="DiscordChannel" class="label">Discord频道Webhook</label>
<nb-select selected="1" style="width: 100%;max-width: unset;" id="DiscordChannelWebhook"
[(ngModel)]="bridge.discord.id" (ngModelChange)="selectWebhookChange(bridge)">
<nb-option [value]="item.id" *ngFor="let item of discordInfo.webhooks">{{item.name}}</nb-option>
<nb-option [value]="item.id" *ngFor="let item of getChannelGuild(bridge.discord.channelID).webhooks">{{item.name}}</nb-option>
</nb-select>
</div>
</ng-container>
<ng-container *ngIf="!discordInfo.hasManageWebhooks">
<ng-container *ngIf="!getChannelGuild(bridge.discord.channelID)?.hasManageWebhooks">
<div class="form-group">
<label for="DiscordChannel" class="label">Discord频道机器人ID</label>
<input type="text" nbInput fullWidth id="DiscordBotID" [(ngModel)]="bridge.discord.id">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
AdminService,
BridgeConfig,
Config,
DiscordAllGuildAndChannelsInfo,
DiscordAllGuildAndChannelsInfo, DiscordGuildInfo,
KHLAllInfo,
QQAllInfo
} from './admin.service';
Expand Down Expand Up @@ -98,7 +98,13 @@ export class BridgeConfigComponent implements OnInit {
}

selectWebhookChange(bridge: BridgeConfig) {
const webhook = this.discordInfo.webhooks.find(webhook => webhook.id === bridge.discord.id);
const webhook = this.getChannelGuild(bridge.discord.channelID).webhooks.find(webhook => webhook.id === bridge.discord.id);
bridge.discord.token = webhook.token;
}

getChannelGuild(channelID: string): DiscordGuildInfo {
return this.discordInfo.guild.find((guild) => {
return guild.channels.find(channel => channel.id === channelID);
})
}
}
29 changes: 16 additions & 13 deletions src/bridge/bridge.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BotService } from '../el-bot/bot.service';
import config, { Config } from '../config';
import * as fs from 'fs';
import * as path from 'path';
import { Collection, Snowflake, Webhook } from 'discord.js';

@Controller('/api/bridge')
export class BridgeController {
Expand Down Expand Up @@ -101,18 +102,14 @@ export class BridgeController {
@Get('discordAllGuildAndChannelsInfo')
async getDiscordAllGuildAndChannelsInfo(@Res() res: Response) {
const result: DiscordAllInfo = {
guild: [],
webhooks: [],
hasManageWebhooks: false
guild: []
}
const guildList = BotService.discord.guilds.cache.array();
for (const guild of guildList) {
result.hasManageWebhooks = guild.me.hasPermission('MANAGE_WEBHOOKS')
if (result.hasManageWebhooks) {
const webhooks = await guildList[0].fetchWebhooks();
webhooks.forEach((webhook) => {
result.webhooks.push({id: webhook.id, name: webhook.name, token: webhook.token});
})
const hasManageWebhooks = guild.me.hasPermission('MANAGE_WEBHOOKS')
let webhooks: Collection<Snowflake, Webhook> = new Collection();
if (hasManageWebhooks) {
webhooks = await guild.fetchWebhooks();
}
const channels = [];
BotService.discord.guilds.cache.get(guild.id).channels.cache.forEach((value, key, map) => {
Expand All @@ -121,7 +118,13 @@ export class BridgeController {
}
})
result.guild.push({
id: guild.id, name: guild.name, channels
id: guild.id,
name: guild.name,
channels,
hasManageWebhooks,
webhooks: webhooks.array().map((webhook) => {
return {id: webhook.id, name: webhook.name, token: webhook.token}
})
})
}
res.status(200).json({data: result});
Expand Down Expand Up @@ -185,8 +188,8 @@ interface DiscordAllInfo {
guild: Array<{
id: string,
name: string
channels: Array<{ id: string, name: string }>
channels: Array<{ id: string, name: string }>;
hasManageWebhooks: boolean;
webhooks: Array<{ id: string, name: string, token: string }>
}>;
hasManageWebhooks: boolean;
webhooks: Array<{ id: string, name: string, token: string }>
}

0 comments on commit cb132d7

Please sign in to comment.