所以我想搜索一个文本频道,然后在它不存在的情况下创建它。我有这个代码,通过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;
}