Add new Hook "OnChannelDelete"Solved

Hey 👋

I'm currently working on a plugin that requires to know when a channel gets deleted, so I've added this hook in order to make it work.

private void Discord_ChannelDelete(Channel channel)
{
	// Delete all subscriptions to this channel for all plugins
	List<ChannelSubscription> subscriptions = _channelSubscriptions[channel.id];
	subscriptions?.RemoveAll(s => true);

	// Delete the channel from the guild if the Channel was deleted by an user from the Discord App
	Channel deletedChannel = _discordServer.channels.First(c => c.id == channel.id);
	_discordServer.channels.Remove(deletedChannel);

	Interface.Oxide.CallHook("OnChannelDelete", channel);
}
Hi evlad,

Thanks for your suggestion. I agree that we need to remove the subscripton when the channel is deleted. The code I have below is what I believe we should add to DiscordCore. The channel is automaically deleted from the _discordServer.channels before the Discord_ChannelDelete hook is called by the Discord Extension. Then you can just add Discord_ChannelDelete hook into the plugin you're creating and I think that will cover everything. Let me know if this would work for you.

private void Discord_ChannelDelete(Channel channel)
{
    // Delete all subscriptions to this channel for all plugins
    _channelSubscriptions.Remove(channel.id);
}​

Thanks,
MJSU

The problem is when a Channel gets deleted `_discordServer.channels` does not update, that's why I added this:

// Delete the channel from the guild if the Channel was deleted by an user from the Discord App
Channel deletedChannel = _discordServer.channels.First(c => c.id == channel.id);
_discordServer.channels.Remove(deletedChannel);

And added "OnChannelDelete" hook because I couldnt get "Discord_ChannelDelete" to work

Interface.Oxide.CallHook("OnChannelDelete", channel);
I just talked with the Discord Extensions author looks like the removing not happening is a bug. That will be getting fixed. I will add it for now until that update is released. The Discord_ChannelDelete hook should work in your plugin. Make sure you make the method private and not public. If it still doesn't work out I can add the hook for you in DiscordCore.

Alright!

 

I've tried this on my plugin:

private void Discord_ChannelDelete(Channel channel)
{
	Puts("DEBUG - Discord channel deleted: " + channel.name);
}

But never gets triggered

I talked to the discord extension dev again. It looks like plugins that don't register a discord client don't get the hooks to be called on them. I'm going to add a way in DiscordCore so a plugin can register to receive those Discord Extension hooks. So you will need to add the below code to your plugin and you will get the discord hooks for your plugin.

private void OnServerInitialized()
{
    DiscordCore.Call("RegisterPluginForExtensionHooks", this);
}​


Merged post

I have push the update containing everything discussed. Please let me know if you have any issues or need anything else added.
Everything works like a charm!

Thanks for actively maintaining the plugin!
Locked automatically