QQChannelBot 1.3.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package QQChannelBot --version 1.3.4                
NuGet\Install-Package QQChannelBot -Version 1.3.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QQChannelBot" Version="1.3.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add QQChannelBot --version 1.3.4                
#r "nuget: QQChannelBot, 1.3.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install QQChannelBot as a Cake Addin
#addin nuget:?package=QQChannelBot&version=1.3.4

// Install QQChannelBot as a Cake Tool
#tool nuget:?package=QQChannelBot&version=1.3.4                

QQ机器人框架

NuGet NuGet downloads

使用.NET6技术封装的QQ机器人通信框架,无需任何第三方依赖。
傻瓜式封装,极简的调用接口,让新手也能快速入门!

使用说明

1.配置日志输出等级(默认值:LogLevel.Info)

Log.LogLevel = LogLevel.Debug;

2.创建机器人, 配置参数请查阅 QQ机器管理后台

BotClient bot = new(new()
{
    BotAppId = 开发者ID,
    BotToken = 机器人令牌,
    BotSecret = 机器人密钥
});

3.订阅 OnReady 事件,这里根据机器人信息修改控制台标题

bot.OnReady += (sender) =>
{
    var sdk = typeof(BotClient).Assembly.GetName();
    Console.Title = $"{sender?.UserName}{(bot.DebugBot ? "-DEBUG" : "")} <{sender?.Id}> - SDK版本:{sdk.Name}_{sdk.Version}";
};

4.如何接收 @机器人 消息(注:OnAtMessage 事件已弃用)

// 订阅消息创建事件,处理所有收到的消息
// 注1:如果该消息包含 @机器人 标签,则isAt=true
// 注2:被 AddCommand 指令命中的消息不会出现在这里
// sender是Message对象(自动内嵌BotClient对象,以支持快速回复)
// 这里示例快速回复用户的AT消息,并修改 @机器人 为 @用户
bot.OnMsgCreate += async (sender, isAt) =>
{
    if (isAt)
    {
        string replyMsg = sender.Content.Replace(MsgTag.User(bot.Info!.Id), MsgTag.User(sender.Author.Id));
        await sender.ReplyAsync(replyMsg);
    }
};

5.注册自定义消息命令

// 注册自定义命令,这里让机器人复读用户的消息
// 如:用户发送 @机器人 复读 123
//   机器回复 123
bot.AddCommand(new Command("复读", async (sender, msg) =>
{
    await sender.ReplyAsync($"{MsgTag.User(sender.Author.Id)} {msg}");
}));

6.启动机器人(开始运行并监听频道消息)

bot.Start();

以下是API调用玩法示例

注:AddCommand注册指令;点此查看Command指令对象详细参数
// 指令格式:@机器人 菜单
// 注:这里排除了Command属性Note有内容的指令
bot.AddCommand(new Command("菜单", async (sender, args) =>
{
    await sender.ReplyAsync(string.join('、', bot.GetCommands.Where(cmd => string.IsNullOrWhiteSpace(cmd.Note)).Select(cmd => cmd.Name).ToList()));
}, note: "hide"));
// 注册自定义命令,这里测试embed消息 ( 实现功能为获取用户信息,指令格式: @机器人 UserInfo @用户 )
bot.AddCommand(new Command("用户信息", async (sender, msg) =>
{
    string userId = Regex.IsMatch(msg, @"<@!(\d+)>") ? Regex.Match(msg, @"<@!(\d+)>").Groups[1].Value : sender.Author.Id;
    Member member = await bot.GetMemberAsync(sender.GuildId, userId) ?? new()
    {
        User = sender.Author,
        Roles = sender.Member.Roles,
        JoinedAt = sender.Member.JoinedAt,
    };
    List<Role>? roles = await bot.GetRolesAsync(sender.GuildId);
    List<string> roleNames = roles?.Where(role => member.Roles.Contains(role.Id)).Select(role => role.Name).ToList() ?? new List<string> { "无权获取身份组列表" };
    MsgEmbed ReplyEmbed = new MsgEmbed()
    {
        Title = member.User!.UserName,
        Prompt = $"{member.User.UserName} 的信息卡",
        Thumbnail = member.User.Avatar
    }
    .AddLine($"用户昵称:{member.Nick}")
    .AddLine($"账户类别:{(member.User.Bot ? "机器人" : "人类")}")
    .AddLine($"角色分类:{string.Join("、", roleNames)}")
    .AddLine($"加入时间:{member.JoinedAt!.Remove(member.JoinedAt.IndexOf('+'))}");
    await sender.ReplyAsync(ReplyEmbed);
}));
// 指令格式:@机器人 创建公告 公告内容
bot.AddCommand(new Command("创建公告", async (sender, args) =>
{
    if (string.IsNullOrWhiteSpace(args))
    {
        await sender.ReplyAsync($"{MsgTag.User(sender.Author.Id)} 未指定公告内容!\n正确格式:@机器人 创建公告 公告内容");
        return;
    }
    Message? sendmsg = await sender.ReplyAsync(args);
    await bot.CreateAnnouncesAsync(sendmsg!);
}, needAdmin: true));
// 指令格式:@机器人 删除公告
bot.AddCommand(new Command("删除公告", async (sender, args) =>
{
    await bot.DeleteAnnouncesAsync(sender.ChannelId);
}, needAdmin: true));
// 指令格式:@机器人 创建全局公告 公告内容
bot.AddCommand(new Command("创建全局公告", async (sender, args) =>
{
    if (string.IsNullOrWhiteSpace(args))
    {
        await sender.ReplyAsync($"{MsgTag.User(sender.Author.Id)} 未指定公告内容!\n正确格式:@机器人 创建全局公告 公告内容");
        return;
    }
    Message? sendmsg = await sender.ReplyAsync(args);
    await bot.CreateAnnouncesGlobalAsync(sendmsg!);
}, needAdmin: true));
// 指令格式:@机器人 删除全局公告
bot.AddCommand(new Command("删除全局公告", async (sender, args) =>
{
    await bot.DeleteAnnouncesGlobalAsync(sender.ChannelId);
}, needAdmin: true));
// 指令格式:@机器人 禁言 @用户 10天(或:到2077-12-12 23:59:59)
bot.AddCommand(new Command("禁言", async (sender, args) =>
{
    Match userIdMatcher = Regex.Match(args, @"<@!(\d+)>");
    string? userId = userIdMatcher.Success ? userIdMatcher.Groups[1].Value : null;
    if (userId == null)
    {
        await sender.ReplyAsync($"{MsgTag.User(sender.Author.Id)} 未指定禁言的用户!\n正确格式:@机器人 禁言 @用户 禁言时间");
        return;
    }
    Match tsm = Regex.Match(args, @"(\d{4})[-年](\d\d)[-月](\d\d)[\s日]*(\d\d)[:点时](\d\d)[:分](\d\d)秒?");
    if (tsm.Success)
    {
        string timeStampStr = tsm.Groups[0].Value;
        bool isOk = await bot.MuteMemberAsync(sender.GuildId, userId, new MuteMaker(timeStampStr));
        if (isOk) await sender.ReplyAsync($"{sender.Mentions!.Find(u => u.Id == userId)?.UserName} 已被禁言,解除时间:{timeStampStr}");
        else await sender.ReplyAsync($"禁言失败,可能没有权限!");
    }
    else
    {
        Match tdm = Regex.Match(args, @"(\d+)\s*(年|星期|周|日|天|小?时|分钟?|秒钟?)");
        bool isOk = await bot.MuteMemberAsync(sender.GuildId, userId, tdm.Success ? new MuteMaker(tdm.Groups[0].Value) : new MuteTime(60));
        if (isOk) await sender.ReplyAsync($"{sender.Mentions!.Find(u => u.Id == userId)?.UserName} 已被禁言{(tdm.Success ? tdm.Groups[0] : "1分钟")}");
        else await sender.ReplyAsync($"禁言失败,可能没有权限!");
    }
}, needAdmin: true));
// 指令格式:@机器人 解除禁言 @用户
bot.AddCommand(new Command("解除禁言", async (sender, args) =>
{
    Match userIdMatcher = Regex.Match(args, @"<@!(\d+)>");
    string? userId = userIdMatcher.Success ? userIdMatcher.Groups[1].Value : null;
    if (userId == null)
    {
        await sender.ReplyAsync($"{MsgTag.User(sender.Author.Id)} 未指定解禁的用户!\n正确格式:@机器人 解禁 @用户");
        return;
    }
    bool isOk = await bot.MuteMemberAsync(sender.GuildId, userId, new MuteTime(0));
    if (isOk) await sender.ReplyAsync($"{sender.Mentions!.Find(u => u.Id == userId)?.UserName} 已解除禁言");
    else await sender.ReplyAsync($"解除禁言失败,可能没有权限!");
}, needAdmin: true));
// 指令格式:@机器人 全体禁言 10天(或:到2077年12月12日23点59分59秒)
bot.AddCommand(new Command("全员禁言", async (sender, args) =>
{
    Match tsm = Regex.Match(args, @"(\d{4})[-年](\d\d)[-月](\d\d)[\s日]*(\d\d)[:点时](\d\d)[:分](\d\d)秒?");
    if (tsm.Success)
    {
        string timeStampStr = tsm.Groups[0].Value;
        bool isOk = await bot.MuteGuildAsync(sender.GuildId, new MuteMaker(timeStampStr));
        if (isOk) await sender.ReplyAsync($"已启用全员禁言,解除时间:{timeStampStr}");
        else await sender.ReplyAsync($"全员禁言失败,可能没有权限!");
    }
    else
    {
        Match tdm = Regex.Match(args, @"(\d+)\s*(年|星期|周|日|天|小?时|分钟?|秒钟?)");
        bool isOk = await bot.MuteGuildAsync(sender.GuildId, tdm.Success ? new MuteMaker(tdm.Groups[0].Value) : new MuteTime(60));
        if (isOk) await sender.ReplyAsync($"已启用全员禁言{(tdm.Success ? tdm.Groups[0] : "1分钟")}");
        else await sender.ReplyAsync($"全员禁言失败,可能没有权限!");
    }
}, needAdmin: true));
// 指令格式:@机器人 解除全体禁言
bot.AddCommand(new Command("解除全员禁言", async (sender, args) =>
{
    bool isOk = await bot.MuteGuildAsync(sender.GuildId, new MuteTime(0));
    if (isOk) await sender.ReplyAsync($"已解除全员禁言");
    else await sender.ReplyAsync($"解除全员禁言失败,可能没有权限!");
}, needAdmin: true));

Ark模板消息构建方法

注:模板消息的构造支持多种方式,以下尽量展示了不同的构造方式,也可以混合使用各种构造方式。
bot.AddCommand(new Command("ark测试", async (sender, args) =>
{
    // Ark23测试通过
    await sender.ReplyAsync(new MsgArk23()
        .SetDesc("描述")
        .SetPrompt("提示消息")
        .AddLine("第一行内容")
        .AddLine("第二行内容")
        .AddLine("百度")
        .AddLine("淘宝")
        .AddLine("腾讯")
        .AddLine("微软")
        .AddLine("最后一行"));

    // Ark24测试通过
    await sender.ReplyAsync(new MsgArk24()
        .SetDesc("描述")
        .SetPrompt("提示")
        .SetTitle("标题")
        .SetMetaDesc("详情")
        .SetImage("")
        .SetLink("")
        .SetSubTitle("子标题"));

    // Ark34测试通过
    await sender.ReplyAsync(new MsgArk34()
    {
        Desc = "描述",
        Prompt = "提示",
        MetaTitle = "标题",
        MetaDesc = "详情",
        MetaIcon = "",
        MetaPreview = "",
        MetaUrl = ""
    });

    // Ark37测试通过
    await sender.ReplyAsync(new MsgArk37("提示", "标题", "子标题"));
}));

更多玩法请参考 Benchmarks

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.2 575 3/4/2022
1.6.1 419 2/19/2022
1.6.0 430 1/30/2022
1.5.4 463 1/22/2022
1.5.3 423 1/21/2022
1.5.2 458 1/20/2022
1.5.1 463 1/18/2022
1.5.0 466 1/18/2022
1.4.2 436 1/16/2022
1.4.1 463 1/14/2022
1.4.0 423 1/13/2022
1.3.9 431 1/12/2022
1.3.8 440 1/11/2022
1.3.7 279 1/10/2022
1.3.6 297 1/10/2022
1.3.5 256 1/9/2022
1.3.4 250 1/8/2022
1.3.3 253 1/6/2022
1.3.2 271 1/4/2022
1.3.1 260 1/3/2022
1.3.0 282 1/2/2022
1.2.9 255 1/2/2022
1.2.8 259 1/1/2022
1.2.7 275 12/31/2021
1.2.6 258 12/30/2021
1.2.5 253 12/30/2021
1.2.4 259 12/30/2021
1.2.3 229 12/29/2021
1.2.2 254 12/28/2021
1.2.1 252 12/28/2021
1.2.0 260 12/27/2021
1.1.3 249 12/26/2021
1.1.2 267 12/26/2021
1.1.1 250 12/26/2021
1.1.0 266 12/24/2021
1.0.9 307 12/24/2021
1.0.8 244 12/23/2021
1.0.7 258 12/23/2021
1.0.6 250 12/23/2021
1.0.5 257 12/23/2021
1.0.4 252 12/22/2021
1.0.3 271 12/22/2021
1.0.2 268 12/21/2021
1.0.1 268 12/20/2021
1.0.0 237 12/20/2021

重构对象属性类型为直接类型,无需再次转换;优化异步等待机制,避免await死锁;