✨ Add guild channel voice states manager
This commit is contained in:
		
							parent
							
								
									d2cedeceb1
								
							
						
					
					
						commit
						51b1d71e9a
					
				
					 3 changed files with 73 additions and 0 deletions
				
			
		
							
								
								
									
										39
									
								
								src/managers/guildChannelVoiceStates.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/managers/guildChannelVoiceStates.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,39 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import { BaseChildManager } from './baseChild.ts' | ||||
| import { VoiceStatePayload } from '../types/voice.ts' | ||||
| import { VoiceState } from '../structures/voiceState.ts' | ||||
| import { GuildVoiceStatesManager } from './guildVoiceStates.ts' | ||||
| import { VoiceChannel } from '../structures/guildVoiceChannel.ts' | ||||
| 
 | ||||
| export class GuildChannelVoiceStatesManager extends BaseChildManager< | ||||
|   VoiceStatePayload, | ||||
|   VoiceState | ||||
| > { | ||||
|   channel: VoiceChannel | ||||
| 
 | ||||
|   constructor( | ||||
|     client: Client, | ||||
|     parent: GuildVoiceStatesManager, | ||||
|     channel: VoiceChannel | ||||
|   ) { | ||||
|     super(client, parent as any) | ||||
|     this.channel = channel | ||||
|   } | ||||
| 
 | ||||
|   async get(id: string): Promise<VoiceState | undefined> { | ||||
|     const res = await this.parent.get(id) | ||||
|     if (res !== undefined && res.channel?.id === this.channel.id) return res | ||||
|     else return undefined | ||||
|   } | ||||
| 
 | ||||
|   async array(): Promise<VoiceState[]> { | ||||
|     const arr = (await this.parent.array()) as VoiceState[] | ||||
|     return arr.filter((c: any) => c.channel?.id === this.channel.id) as any | ||||
|   } | ||||
| 
 | ||||
|   async fromPayload(d: VoiceStatePayload[]): Promise<void> { | ||||
|     for (const data of d) { | ||||
|       await this.set(data.user_id, data) | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | @ -10,6 +10,7 @@ import { CHANNEL } from '../types/endpoint.ts' | |||
| import { GuildChannel } from './channel.ts' | ||||
| import { Guild } from './guild.ts' | ||||
| import { VoiceState } from './voiceState.ts' | ||||
| import { GuildChannelVoiceStatesManager } from '../managers/guildChannelVoiceStates.ts' | ||||
| 
 | ||||
| export interface VoiceServerData extends VoiceServerUpdateData { | ||||
|   sessionID: string | ||||
|  | @ -18,6 +19,11 @@ export interface VoiceServerData extends VoiceServerUpdateData { | |||
| export class VoiceChannel extends GuildChannel { | ||||
|   bitrate: string | ||||
|   userLimit: number | ||||
|   voiceStates = new GuildChannelVoiceStatesManager( | ||||
|     this.client, | ||||
|     this.guild.voiceStates, | ||||
|     this | ||||
|   ) | ||||
| 
 | ||||
|   constructor(client: Client, data: GuildVoiceChannelPayload, guild: Guild) { | ||||
|     super(client, data, guild) | ||||
|  |  | |||
							
								
								
									
										28
									
								
								src/test/cmds/kickFromSpecificVoice.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/test/cmds/kickFromSpecificVoice.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,28 @@ | |||
| import { Command, VoiceChannel } from '../../../mod.ts' | ||||
| import { CommandContext } from '../../models/command.ts' | ||||
| import { ChannelTypes } from '../../types/channel.ts' | ||||
| 
 | ||||
| export default class KickFromSpecificVoiceCommand extends Command { | ||||
|   name = 'kickFromSpecificVoice' | ||||
| 
 | ||||
|   async execute(ctx: CommandContext): Promise<void> { | ||||
|     if (ctx.guild !== undefined) { | ||||
|       const channel = await ctx.guild.channels.get('YOUR VOICE CHANNEL ID') | ||||
|       if (channel === undefined || channel.type !== ChannelTypes.GUILD_VOICE) { | ||||
|         ctx.channel.send('The channel is either not a voice or not available.') | ||||
|         return | ||||
|       } | ||||
| 
 | ||||
|       // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
 | ||||
|       const voiceStates = await (channel as VoiceChannel).voiceStates.array() | ||||
|       if (voiceStates !== undefined) { | ||||
|         voiceStates.forEach(async (voiceState) => { | ||||
|           const member = await voiceState.disconnect() | ||||
|           if (member !== undefined) { | ||||
|             ctx.channel.send(`Kicked member ${member.id}`) | ||||
|           } | ||||
|         }) | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue