如果 Discord.Net C# 中不存在,如何按名称查找并创建频道
How to find a channel by name and create it if it does not exist in Discord.Net C#
所以我想搜索一个文本频道,如果不存在则创建它。
我有通过 ID 获取它的代码,但如果频道不存在,则没有 ID。如果按名称搜索频道不存在,如何创建它?
// here needs to be finding the channel etc.
ITextChannel logChannel = Context.Client.GetChannel(ChannelID) as ITextChannel;
If (!logChannel.Exists)
{
await Context.Guild.CreateTextChannelAsync("log");
}
您可以使用 Linq 的强大功能搜索频道,如果找不到频道,您可以按照上面写的方式创建频道:
// find the channel if exists (by any criteria, in your case, check by the name)
var channel = Context.Guild.Channels.SingleOrDefault(x => x.Name == "log");
if (channel == null) // there is no channel with the name of 'log'
{
// create the channel
var newChannel = await Context.Guild.CreateTextChannelAsync("log");
// If you need the newly created channels id
var newChannelId = newChannel.Id;
}
所以我想搜索一个文本频道,如果不存在则创建它。 我有通过 ID 获取它的代码,但如果频道不存在,则没有 ID。如果按名称搜索频道不存在,如何创建它?
// here needs to be finding the channel etc.
ITextChannel logChannel = Context.Client.GetChannel(ChannelID) as ITextChannel;
If (!logChannel.Exists)
{
await Context.Guild.CreateTextChannelAsync("log");
}
您可以使用 Linq 的强大功能搜索频道,如果找不到频道,您可以按照上面写的方式创建频道:
// find the channel if exists (by any criteria, in your case, check by the name)
var channel = Context.Guild.Channels.SingleOrDefault(x => x.Name == "log");
if (channel == null) // there is no channel with the name of 'log'
{
// create the channel
var newChannel = await Context.Guild.CreateTextChannelAsync("log");
// If you need the newly created channels id
var newChannelId = newChannel.Id;
}