diff --git a/README.md b/README.md index 17a8b05..580e0b3 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ func main() { Handle(func(ctx *nano.Ctx) { _, _ = ctx.SendPlainMessage(false, "echo string") }) - nano.Run(&nano.Bot{ + nano.Run(nil, &nano.Bot{ AppID: "你的AppID", Token: "你的Token", Secret: "你的Secret, 目前没用到, 可以不填", @@ -80,7 +80,7 @@ import ( func main() { log.SetLevel(log.DebugLevel) nano.OpenAPI = nano.SandboxAPI - nano.Run(&nano.Bot{ + nano.Run(nil, &nano.Bot{ AppID: "你的AppID", Token: "你的Token", Secret: "你的Secret, 目前没用到, 可以不填", diff --git a/api_generated.go b/api_generated.go new file mode 100644 index 0000000..edeaa30 --- /dev/null +++ b/api_generated.go @@ -0,0 +1,433 @@ +// Code generated by codegen/context. DO NOT EDIT. + +package nano + +// 生成自文件 openapi_announces.go + +// PostAnnounceInGuild 创建频道全局公告,公告类型分为 消息类型的频道公告 和 推荐子频道类型的频道公告 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/announces/post_guild_announces.html +// +// 会重写 content 为返回值 +func (ctx *Ctx) PostAnnounceInGuild(id string, content *Announces) error { + return ctx.caller.PostAnnounceInGuild(id, content) +} + +// DeleteAnnounceInGuild 删除频道 guild_id 下指定 message_id 的全局公告 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/announces/delete_guild_announces.html +// +// message_id 有值时,会校验 message_id 合法性,若不校验校验 message_id,请将 message_id 设置为 all +func (ctx *Ctx) DeleteAnnounceInGuild(guildid, messageid string) error { + return ctx.caller.DeleteAnnounceInGuild(guildid, messageid) +} +// 生成自文件 openapi_audio.go + +// ControlAudioInChannel 控制子频道 channel_id 下的音频 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/audio/audio_control.html +func (ctx *Ctx) ControlAudioInChannel(id string, control *AudioControl) error { + return ctx.caller.ControlAudioInChannel(id, control) +} + +// OpenMic 机器人在 channel_id 对应的语音子频道上麦 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/audio/put_mic.html +func (ctx *Ctx) OpenMicInChannel(id string) error { + return ctx.caller.OpenMicInChannel(id) +} + +// CloseMicInChannel 机器人在 channel_id 对应的语音子频道下麦 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/audio/delete_mic.html +func (ctx *Ctx) CloseMicInChannel(id string) error { + return ctx.caller.CloseMicInChannel(id) +} +// 生成自文件 openapi_channel.go + +// GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html +func (ctx *Ctx) GetChannelsOfGuild(id string) (channels []Channel, err error) { + return ctx.caller.GetChannelsOfGuild(id) +} + +// GetChannelByID 用于获取 channel_id 指定的子频道的详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channel.html +func (ctx *Ctx) GetChannelByID(id string) (*Channel, error) { + return ctx.caller.GetChannelByID(id) +} + +// CreateChannelInGuild 用于在 guild_id 指定的频道下创建一个子频道 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/post_channels.html +func (ctx *Ctx) CreateChannelInGuild(id string, config *ChannelPost) (*Channel, error) { + return ctx.caller.CreateChannelInGuild(id, config) +} + +// PatchChannelOf 修改 channel_id 指定的子频道的信息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/patch_channel.html +func (ctx *Ctx) PatchChannelOf(id string, config *ChannelPatch) (*Channel, error) { + return ctx.caller.PatchChannelOf(id, config) +} + +// DeleteChannelOf 删除 channel_id 指定的子频道 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/delete_channel.html +func (ctx *Ctx) DeleteChannelOf(id string) error { + return ctx.caller.DeleteChannelOf(id) +} + +// GetOnlineNumsInChannel 查询音视频/直播子频道 channel_id 的在线成员数 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_online_nums.html +func (ctx *Ctx) GetOnlineNumsInChannel(id string) (int, error) { + return ctx.caller.GetOnlineNumsInChannel(id) +} + +// GetChannelPermissionsOfUser 获取子频道 channel_id 下用户 user_id 的权限 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/get_channel_permissions.html +func (ctx *Ctx) GetChannelPermissionsOfUser(channelid, userid string) (*ChannelPermissions, error) { + return ctx.caller.GetChannelPermissionsOfUser(channelid, userid) +} + +// SetChannelPermissionsOfUser 修改子频道 channel_id 下用户 user_id 的权限 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/put_channel_permissions.html +func (ctx *Ctx) SetChannelPermissionsOfUser(channelid, userid string, add, remove string) error { + return ctx.caller.SetChannelPermissionsOfUser(channelid, userid, add, remove) +} + +// GetChannelPermissionsOfRole 获取子频道 channel_id 下身份组 role_id 的权限 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/get_channel_roles_permissions.html +func (ctx *Ctx) GetChannelPermissionsOfRole(channelid, roleid string) (*ChannelPermissions, error) { + return ctx.caller.GetChannelPermissionsOfRole(channelid, roleid) +} + +// SetChannelPermissionsOfRole 修改子频道 channel_id 下身份组 role_id 的权限 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/put_channel_roles_permissions.html +func (ctx *Ctx) SetChannelPermissionsOfRole(channelid, roleid string, add, remove string) error { + return ctx.caller.SetChannelPermissionsOfRole(channelid, roleid, add, remove) +} +// 生成自文件 openapi_codegen_getopenapiof.go +// 生成自文件 openapi_codegen_patchopenapiof.go +// 生成自文件 openapi_codegen_postopenapiof.go +// 生成自文件 openapi_codegen_putopenapiof.go +// 生成自文件 openapi_dms.go + +// CreatePrivateChat 机器人和在同一个频道内的成员创建私信会话 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/post_dms.html +func (ctx *Ctx) CreatePrivateChat(guildid, userid string) (*DMS, error) { + return ctx.caller.CreatePrivateChat(guildid, userid) +} + +// PostMessageToUser 发送私信消息,前提是已经创建了私信会话 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/post_dms_messages.html +// +// - 私信的 guild_id 在创建私信会话时以及私信消息事件中获取 +func (ctx *Ctx) PostMessageToUser(id string, content *MessagePost) (*Message, error) { + return ctx.caller.PostMessageToUser(id, content) +} + +// DeleteMessageOfUser 撤回私信频道 guild_id 中 message_id 指定的私信消息, 只能用于撤回机器人自己发送的私信 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/delete_dms.html +func (ctx *Ctx) DeleteMessageOfUser(guildid, messageid string, hidetip bool) error { + return ctx.caller.DeleteMessageOfUser(guildid, messageid, hidetip) +} +// 生成自文件 openapi_emoji.go + +// GiveMessageReaction 对消息 message_id 进行表情表态 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/reaction/put_message_reaction.html +func (ctx *Ctx) GiveMessageReaction(channelid, messageid string, emoji Emoji) error { + return ctx.caller.GiveMessageReaction(channelid, messageid, emoji) +} + +// DeleteMessageReaction 删除自己对消息 message_id 的表情表态 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/reaction/delete_own_message_reaction.html +func (ctx *Ctx) DeleteMessageReaction(channelid, messageid string, emoji Emoji) error { + return ctx.caller.DeleteMessageReaction(channelid, messageid, emoji) +} + +// GetMessageReactionUsers 拉取对消息 message_id 指定表情表态的用户列表 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/reaction/get_reaction_users.html +func (ctx *Ctx) GetMessageReactionUsers(channelid, messageid string, emoji Emoji, cookie string, limit int) (*MessageReactionUsers, error) { + return ctx.caller.GetMessageReactionUsers(channelid, messageid, emoji, cookie, limit) +} +// 生成自文件 openapi_forum.go + +// GetChannelThreads 获取子频道下的帖子列表 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/forum/get_threads_list.html +func (ctx *Ctx) GetChannelThreads(id string) (threads []Thread, isfinish bool, err error) { + return ctx.caller.GetChannelThreads(id) +} + +// GetThreadInfo 获取子频道下的帖子详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/forum/get_thread.html +func (ctx *Ctx) GetThreadInfo(channelid, threadid string) (*ThreadInfo, error) { + return ctx.caller.GetThreadInfo(channelid, threadid) +} + +// PostThread 发表帖子 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/forum/put_thread.html +func (ctx *Ctx) PostThreadInChannel(id string, title string, content string, format uint32) (taskid string, createtime string, err error) { + return ctx.caller.PostThreadInChannel(id, title, content, format) +} + +// DeleteThreadInChannel 删除指定子频道下的某个帖子 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/forum/delete_thread.html +func (ctx *Ctx) DeleteThreadInChannel(channelid, threadid string) error { + return ctx.caller.DeleteThreadInChannel(channelid, threadid) +} +// 生成自文件 openapi_guild.go + +// GetGuildByID 获取 guild_id 指定的频道的详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild.html +func (ctx *Ctx) GetGuildByID(id string) (*Guild, error) { + return ctx.caller.GetGuildByID(id) +} + +// SetAllMuteInGuild 禁言全员 / 解除全员禁言 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_mute.html +func (ctx *Ctx) SetAllMuteInGuild(id string, endtimestamp string, seconds string) error { + return ctx.caller.SetAllMuteInGuild(id, endtimestamp, seconds) +} + +// SetUserMuteInGuild 禁言 / 解除禁言频道 guild_id 下的成员 user_id +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_mute.html +func (ctx *Ctx) SetUserMuteInGuild(guildid, userid string, endtimestamp string, seconds string) error { + return ctx.caller.SetUserMuteInGuild(guildid, userid, endtimestamp, seconds) +} + +// SetUsersMuteInGuild 批量禁言 / 解除禁言频道 guild_id 下的成员 user_id +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_mute.html +func (ctx *Ctx) SetUsersMuteInGuild(guildid string, endtimestamp string, seconds string, userids ...string) ([]string, error) { + return ctx.caller.SetUsersMuteInGuild(guildid, endtimestamp, seconds, userids...) +} +// 生成自文件 openapi_markdown.go +// 生成自文件 openapi_member.go + +// GetGuildMembersIn 获取 guild_id 指定的频道中所有成员的详情列表,支持分页 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_members.html +func (ctx *Ctx) GetGuildMembersIn(id, after string, limit uint32) (members []Member, err error) { + return ctx.caller.GetGuildMembersIn(id, after, limit) +} + +// GetRoleMembersOf 获取 guild_id 频道中指定role_id身份组下所有成员的详情列表,支持分页 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_role_members.html +func (ctx *Ctx) GetRoleMembersOf(guildid, roleid, startindex string, limit uint32) (*RoleMembers, error) { + return ctx.caller.GetRoleMembersOf(guildid, roleid, startindex, limit) +} + +// GetGuildMemberOf 获取 guild_id 指定的频道中 user_id 对应成员的详细信息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_member.html +func (ctx *Ctx) GetGuildMemberOf(guildid, userid string) (*Member, error) { + return ctx.caller.GetGuildMemberOf(guildid, userid) +} + +// DeleteGuildMemberOf 删除 guild_id 指定的频道下的成员 user_id +// +// https://bot.q.qq.com/wiki/develop/api/openapi/member/delete_member.html +// +// - delhistmsgdays: 消息撤回时间范围仅支持固定的天数:3,7,15,30。 特殊的时间范围:-1: 撤回全部消息。默认值为0不撤回任何消息。 +func (ctx *Ctx) DeleteGuildMemberOf(guildid, userid string, addblklst bool, delhistmsgdays int) error { + return ctx.caller.DeleteGuildMemberOf(guildid, userid, addblklst, delhistmsgdays) +} +// 生成自文件 openapi_message.go + +// GetMessageFromChannel 获取子频道 channel_id 下的消息 message_id 的详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/get_message_of_id.html +func (ctx *Ctx) GetMessageFromChannel(messageid, channelid string) (*Message, error) { + return ctx.caller.GetMessageFromChannel(messageid, channelid) +} + +// PostMessageToChannel 向 channel_id 指定的子频道发送消息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/post_messages.html +func (ctx *Ctx) PostMessageToChannel(id string, content *MessagePost) (*Message, error) { + return ctx.caller.PostMessageToChannel(id, content) +} + +// DeleteMessageInChannel 回子频道 channel_id 下的消息 message_id +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/delete_message.html +func (ctx *Ctx) DeleteMessageInChannel(channelid, messageid string, hidetip bool) error { + return ctx.caller.DeleteMessageInChannel(channelid, messageid, hidetip) +} + +// GetGuildMessageSetting 获取机器人在频道 guild_id 内的消息频率设置 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/setting/message_setting.html +func (ctx *Ctx) GetGuildMessageSetting(id string) (*MessageSetting, error) { + return ctx.caller.GetGuildMessageSetting(id) +} +// 生成自文件 openapi_permissions.go +// 生成自文件 openapi_pins.go + +// PinMessageInChannel 添加子频道 channel_id 内的精华消息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/pins/put_pins_message.html +func (ctx *Ctx) PinMessageInChannel(channelid, messageid string) (*PinsMessage, error) { + return ctx.caller.PinMessageInChannel(channelid, messageid) +} + +// UnpinMessageInChannel 子频道 channel_id 下指定 message_id 的精华消息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/pins/delete_pins_message.html +// +// 删除子频道内全部精华消息,请将 message_id 设置为 all +func (ctx *Ctx) UnpinMessageInChannel(channelid, messageid string) error { + return ctx.caller.UnpinMessageInChannel(channelid, messageid) +} + +// GetPinMessagesOfChannel 获取子频道 channel_id 内的精华消息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/pins/get_pins_message.html +func (ctx *Ctx) GetPinMessagesOfChannel(id string) (*PinsMessage, error) { + return ctx.caller.GetPinMessagesOfChannel(id) +} +// 生成自文件 openapi_richobj.go +// 生成自文件 openapi_role.go + +// GetGuildRoleListIn 获取 guild_id 指定的频道下的身份组列表 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild_roles.html +func (ctx *Ctx) GetGuildRoleListIn(id string) (*GuildRoleList, error) { + return ctx.caller.GetGuildRoleListIn(id) +} + +// CreateGuildRoleOf 创建频道身份组 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/post_guild_role.html +// +// 参数为非必填,但至少需要传其中之一,默认为空或 0 +func (ctx *Ctx) CreateGuildRoleOf(id string, name string, color uint32, hoist int32) (*GuildRoleCreate, error) { + return ctx.caller.CreateGuildRoleOf(id, name, color, hoist) +} + +// PatchGuildRoleOf 修改频道 guild_id 下 role_id 指定的身份组 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_role.html +func (ctx *Ctx) PatchGuildRoleOf(guildid, roleid string, name string, color uint32, hoist int32) (*GuildRolePatch, error) { + return ctx.caller.PatchGuildRoleOf(guildid, roleid, name, color, hoist) +} + +// DeleteGuildRoleOf 删除频道 guild_id下 role_id 对应的身份组 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_role.html +func (ctx *Ctx) DeleteGuildRoleOf(guildid, roleid string) error { + return ctx.caller.DeleteGuildRoleOf(guildid, roleid) +} + +// AddRoleToMemberOfGuild 将频道 guild_id 下的用户 user_id 添加到身份组 role_id +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/put_guild_member_role.html +// +// 返回 channel_id +func (ctx *Ctx) AddRoleToMemberOfGuild(guildid, userid, roleid, channelid string) (string, error) { + return ctx.caller.AddRoleToMemberOfGuild(guildid, userid, roleid, channelid) +} + +// RemoveRoleFromMemberOfGuild 将用户 user_id 从 频道 guild_id 的 role_id 身份组中移除 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_member_role.html +func (ctx *Ctx) RemoveRoleFromMemberOfGuild(guildid, userid, roleid, channelid string) error { + return ctx.caller.RemoveRoleFromMemberOfGuild(guildid, userid, roleid, channelid) +} +// 生成自文件 openapi_schedule.go + +// GetChannelSchedules 获取channel_id指定的子频道中当天的日程列表 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/schedule/get_schedules.html +func (ctx *Ctx) GetChannelSchedules(id string, since uint64) (schedules []Schedule, err error) { + return ctx.caller.GetChannelSchedules(id, since) +} + +// GetScheduleInChannel 获取日程子频道 channel_id 下 schedule_id 指定的的日程的详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/schedule/get_schedule.html +func (ctx *Ctx) GetScheduleInChannel(channelid string, scheduleid string) (*Schedule, error) { + return ctx.caller.GetScheduleInChannel(channelid, scheduleid) +} + +// CreateScheduleInChannel 在 channel_id 指定的日程子频道下创建一个日程 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/schedule/post_schedule.html +// +// schedule 会被写入返回的对象 +func (ctx *Ctx) CreateScheduleInChannel(id string, schedule *Schedule) error { + return ctx.caller.CreateScheduleInChannel(id, schedule) +} + +// PatchScheduleInChannel 修改日程子频道 channel_id 下 schedule_id 指定的日程的详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/schedule/patch_schedule.html +// +// schedule 会被写入返回的对象 +func (ctx *Ctx) PatchScheduleInChannel(channelid string, scheduleid string, schedule *Schedule) error { + return ctx.caller.PatchScheduleInChannel(channelid, scheduleid, schedule) +} + +// DeleteScheduleInChannel 删除日程子频道 channel_id 下 schedule_id 指定的日程 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/schedule/delete_schedule.html +func (ctx *Ctx) DeleteScheduleInChannel(channelid string, scheduleid string) error { + return ctx.caller.DeleteScheduleInChannel(channelid, scheduleid) +} +// 生成自文件 openapi_user.go + +// AtMe 返回 <@!bot.ready.User.ID> +func (ctx *Ctx) AtMe() string { + return ctx.caller.AtMe() +} + +// GetMyInfo 获取当前用户(机器人)详情 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/user/me.html +func (ctx *Ctx) GetMyInfo() (*User, error) { + return ctx.caller.GetMyInfo() +} + +// GetMyGuilds 获取当前用户(机器人)频道列表,支持分页 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/user/guilds.html +func (ctx *Ctx) GetMyGuilds(before, after string, limit int) (guilds []Guild, err error) { + return ctx.caller.GetMyGuilds(before, after, limit) +} +// 生成自文件 openapi_wss.go + +// GetGeneralWSSGateway 获取通用 WSS 接入点 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/wss/url_get.html +func (ctx *Ctx) GetGeneralWSSGateway() (string, error) { + return ctx.caller.GetGeneralWSSGateway() +} + +// GetShardWSSGateway 获取带分片 WSS 接入点 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/wss/shard_url_get.html +func (ctx *Ctx) GetShardWSSGateway() (*ShardWSSGateway, error) { + return ctx.caller.GetShardWSSGateway() +} diff --git a/bot.go b/bot.go index 1d9e6da..25f04e9 100644 --- a/bot.go +++ b/bot.go @@ -18,7 +18,10 @@ import ( log "github.com/sirupsen/logrus" ) -var clients = syncx.Map[string, *Bot]{} +var ( + clients = syncx.Map[string, *Bot]{} + isrunning uintptr +) // Bot 一个机器人实例的配置 type Bot struct { @@ -29,8 +32,9 @@ type Bot struct { Timeout time.Duration // Timeout is API 调用超时 Handler *Handler `json:"-"` // Handler 注册对各种事件的处理 Intents uint32 // Intents 欲接收的事件 + ShardIndex uint8 // ShardIndex 本连接为第几个分片, 默认 1, 0 为不使用分片 + ShardCount uint8 // ShardCount 分片总数 shard [2]byte // shard 分片 - ShardIndex uint16 // ShardIndex 本连接为第几个分片, 默认 1, 0 为不使用分片 Properties json.RawMessage // Properties 一些环境变量, 目前没用 gateway string // gateway 获得的网关 @@ -45,6 +49,11 @@ type Bot struct { ready EventReady // ready 连接成功后下发的 bot 基本信息 } +// GetReady 获得 bot 基本信息 +func (ctx *Ctx) GetReady() *EventReady { + return &ctx.caller.ready +} + // getinitinfo 获得 gateway 和 shard func (b *Bot) getinitinfo() (gw string, shard [2]byte, err error) { shard[1] = 1 @@ -62,19 +71,26 @@ func (b *Bot) getinitinfo() (gw string, shard [2]byte, err error) { if err != nil { return } - if sgw.Shards <= int(b.ShardIndex) { + if b.ShardCount == 0 { + log.Infoln(getLogHeader(), "使用网关推荐Shards数:", sgw.Shards) + b.ShardCount = uint8(sgw.Shards) + } + if b.ShardCount <= b.ShardIndex { err = errors.New("shard index " + strconv.Itoa(int(b.ShardIndex)) + " >= suggested size " + strconv.Itoa(sgw.Shards)) return } gw = sgw.URL shard[0] = byte(b.ShardIndex) - shard[1] = byte(sgw.Shards) + shard[1] = byte(b.ShardCount) } return } // Start clients without blocking func Start(bots ...*Bot) error { + if !atomic.CompareAndSwapUintptr(&isrunning, 0, 1) { + log.Warnln(getLogHeader(), "已忽略重复调用的", getThisFuncName()) + } for _, b := range bots { gw, shard, err := b.getinitinfo() if err != nil { @@ -86,7 +102,10 @@ func Start(bots ...*Bot) error { } // Run clients and block self in listening last one -func Run(bots ...*Bot) error { +func Run(preblock func(), bots ...*Bot) error { + if !atomic.CompareAndSwapUintptr(&isrunning, 0, 1) { + log.Warnln(getLogHeader(), "已忽略重复调用的", getThisFuncName()) + } var b *Bot switch len(bots) { case 0: @@ -113,7 +132,11 @@ func Run(bots ...*Bot) error { } b.Init(gw, shard) } - b.Connect().Listen() + b.Connect() + if preblock != nil { + preblock() + } + b.Listen() return nil } @@ -153,11 +176,6 @@ func (bot *Bot) Authorization() string { return "Bot " + bot.AppID + "." + bot.Token } -// AtMe 返回 <@!bot.ready.User.ID> -func (bot *Bot) AtMe() string { - return "<@!" + bot.ready.User.ID + ">" -} - // receive 收一个 payload func (bot *Bot) reveive() (payload WebsocketPayload, err error) { err = bot.conn.ReadJSON(&payload) diff --git a/codegen/context/main.go b/codegen/context/main.go new file mode 100644 index 0000000..3bab2fe --- /dev/null +++ b/codegen/context/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "io/fs" + "os" + "regexp" + "strings" + + nano "github.com/fumiama/NanoBot" +) + +const apirestr = `(\n//\s\w+\s.+\n(//.*\n)*)func\s\(bot\s\*Bot\)\s(.*)\s\{` + +var apire = regexp.MustCompile(apirestr) + +func main() { + f, err := os.Create("api_generated.go") + if err != nil { + panic(err) + } + defer f.Close() + f.WriteString(`// Code generated by codegen/context. DO NOT EDIT. + +package nano + +`) + err = fs.WalkDir(os.DirFS("./"), ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if !strings.HasPrefix(path, "openapi_") { + return nil + } + data, err := os.ReadFile(path) + if err != nil { + return err + } + f.WriteString("// 生成自文件 ") + f.WriteString(path) + f.WriteString("\n") + for _, define := range apire.FindAllStringSubmatch(nano.BytesToString(data), -1) { + f.WriteString(define[1]) // 注释 + f.WriteString("func (ctx *Ctx) ") // 函数声明 + f.WriteString(define[3]) + f.WriteString(" {\n") + // 函数调用 + f.WriteString("\treturn ctx.caller.") + funcname, after, _ := strings.Cut(define[3], "(") + f.WriteString(funcname) + f.WriteString("(") + after, _, _ = strings.Cut(after, ")") + paras := strings.Split(after, ", ") + switch len(paras) { + case 0: + case 1: + name, def, _ := strings.Cut(paras[0], " ") + f.WriteString(name) + if strings.Contains(def, "...") { + f.WriteString("...") + } + default: + for _, para := range paras[:len(paras)-1] { + name, _, _ := strings.Cut(para, " ") + f.WriteString(name) + f.WriteString(", ") + } + name, def, _ := strings.Cut(paras[len(paras)-1], " ") + f.WriteString(name) + if strings.Contains(def, "...") { + f.WriteString("...") + } + } + f.WriteString(")\n}\n") + } + return nil + }) + if err != nil { + panic(err) + } +} diff --git a/context.go b/context.go index e3c43d3..e0e2024 100644 --- a/context.go +++ b/context.go @@ -10,10 +10,11 @@ import ( type Ctx struct { Event State - Caller *Bot Message *Message - ma *Matcher IsToMe bool + + caller *Bot + ma *Matcher } // decoder 反射获取的数据 @@ -88,9 +89,9 @@ func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (*Message, error) { } if msg.SrcGuildID != "" { // dms - return ctx.Caller.PostMessageToUser(msg.GuildID, post) + return ctx.PostMessageToUser(msg.GuildID, post) } - return ctx.Caller.PostMessageToChannel(msg.ChannelID, post) + return ctx.PostMessageToChannel(msg.ChannelID, post) } // SendPlainMessage 发送纯文本消息到对方 @@ -110,9 +111,9 @@ func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message post.Content = HideURL(fmt.Sprint(printable...)) if msg.SrcGuildID != "" { // dms - return ctx.Caller.PostMessageToUser(msg.GuildID, post) + return ctx.PostMessageToUser(msg.GuildID, post) } - return ctx.Caller.PostMessageToChannel(msg.ChannelID, post) + return ctx.PostMessageToChannel(msg.ChannelID, post) } // SendImage 发送带图片消息到对方 @@ -138,9 +139,9 @@ func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (*Mes post.Content = HideURL(fmt.Sprint(caption...)) if msg.SrcGuildID != "" { // dms - return ctx.Caller.PostMessageToUser(msg.GuildID, post) + return ctx.PostMessageToUser(msg.GuildID, post) } - return ctx.Caller.PostMessageToChannel(msg.ChannelID, post) + return ctx.PostMessageToChannel(msg.ChannelID, post) } // Block 匹配成功后阻止后续触发 diff --git a/event.go b/event.go index 1d63979..f686c90 100644 --- a/event.go +++ b/event.go @@ -44,7 +44,7 @@ func (bot *Bot) processEvent(payload *WebsocketPayload) { Seq: payload.S, }, State: State{}, - Caller: bot, + caller: bot, } switch tp { case "DirectMessageCreate": @@ -86,14 +86,14 @@ func match(ctx *Ctx, matchers []*Matcher) { if ctx.Message != nil && ctx.Message.Content != "" { // 确保无空 if !ctx.IsToMe { ctx.IsToMe = func(ctx *Ctx) bool { - name := ctx.Caller.ready.User.Username + name := ctx.GetReady().User.Username if strings.HasPrefix(ctx.Message.Content, name) { log.Debugln(getLogHeader(), "message before process:", ctx.Message.Content) ctx.Message.Content = strings.TrimLeft(ctx.Message.Content[len(name):], " ") log.Debugln(getLogHeader(), "message after process:", ctx.Message.Content) return true } - atme := ctx.Caller.AtMe() + atme := ctx.AtMe() if strings.HasPrefix(ctx.Message.Content, atme) { log.Debugln(getLogHeader(), "message before process:", ctx.Message.Content) ctx.Message.Content = strings.TrimLeft(ctx.Message.Content[len(atme):], " ") diff --git a/example/handler/main.go b/example/handler/main.go index b9d81e9..d295f2d 100644 --- a/example/handler/main.go +++ b/example/handler/main.go @@ -10,7 +10,7 @@ import ( func main() { log.SetLevel(log.DebugLevel) nano.OpenAPI = nano.SandboxAPI - nano.Run(&nano.Bot{ + nano.Run(nil, &nano.Bot{ AppID: "你的AppID", Token: "你的Token", Secret: "你的Secret, 目前没用到, 可以不填", diff --git a/example/main.go b/example/main.go index b69dffa..09f6784 100644 --- a/example/main.go +++ b/example/main.go @@ -14,7 +14,7 @@ func main() { Handle(func(ctx *nano.Ctx) { _, _ = ctx.SendPlainMessage(false, "echo string") }) - nano.Run(&nano.Bot{ + nano.Run(nil, &nano.Bot{ AppID: "你的AppID", Token: "你的Token", Secret: "你的Secret, 目前没用到, 可以不填", diff --git a/go.mod b/go.mod index 48db502..012b3f1 100644 --- a/go.mod +++ b/go.mod @@ -24,23 +24,18 @@ require ( github.com/fumiama/go-simple-protobuf v0.1.0 // indirect github.com/fumiama/gofastTEA v0.0.10 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect golang.org/x/text v0.4.0 // indirect - golang.org/x/tools v0.1.12 // indirect - lukechampine.com/uint128 v1.2.0 // indirect - modernc.org/cc/v3 v3.40.0 // indirect - modernc.org/ccgo/v3 v3.16.13 // indirect modernc.org/libc v1.21.5 // indirect modernc.org/mathutil v1.5.0 // indirect modernc.org/memory v1.4.0 // indirect - modernc.org/opt v0.1.3 // indirect modernc.org/sqlite v1.20.0 // indirect - modernc.org/strutil v1.1.3 // indirect - modernc.org/token v1.0.1 // indirect ) + +replace modernc.org/sqlite => github.com/fumiama/sqlite3 v1.20.0-with-win386 + +replace github.com/remyoudompheng/bigfft => github.com/fumiama/bigfft v0.0.0-20211011143303-6e0bfa3c836b diff --git a/go.sum b/go.sum index bacfd6b..5ad04da 100644 --- a/go.sum +++ b/go.sum @@ -13,7 +13,8 @@ github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5/go.mod h1:0Uc github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/fumiama/bigfft v0.0.0-20211011143303-6e0bfa3c836b h1:Zt3pFQditAdWTHCOVkiloc9ZauBoWrb37guFV4iIRvE= +github.com/fumiama/bigfft v0.0.0-20211011143303-6e0bfa3c836b/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo= github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY= github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA= @@ -24,23 +25,18 @@ github.com/fumiama/go-simple-protobuf v0.1.0 h1:rLzJgNqB6LHNDVMl81yyNt6ZKziWtVfu github.com/fumiama/go-simple-protobuf v0.1.0/go.mod h1:5yYNapXq1tQMOZg9bOIVhQlZk9pQqpuFIO4DZLbsdy4= github.com/fumiama/gofastTEA v0.0.10 h1:JJJ+brWD4kie+mmK2TkspDXKzqq0IjXm89aGYfoGhhQ= github.com/fumiama/gofastTEA v0.0.10/go.mod h1:RIdbYZyB4MbH6ZBlPymRaXn3cD6SedlCu5W/HHfMPBk= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/fumiama/sqlite3 v1.20.0-with-win386 h1:ZR1AXGBEtkfq9GAXehOVcwn+aaCG8itrkgEsz4ggx5k= +github.com/fumiama/sqlite3 v1.20.0-with-win386/go.mod h1:Os58MHwYCcYZCy2PGChBrQtBAw5/LS1ZZOkfc+C/I7s= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= -github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= -github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -56,8 +52,6 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/wdvxdr1123/ZeroBot v1.7.5-0.20231009162356-57f71b9f5258 h1:Q0dKoj9SHrR8WjjlcX+eyYBjQKqBn/x1pdJJO1IIOxQ= github.com/wdvxdr1123/ZeroBot v1.7.5-0.20231009162356-57f71b9f5258/go.mod h1:y29UIOy0RD3P+0meDNIWRhcJF3jtWPN9xP9hgt/AJAU= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc= @@ -66,34 +60,14 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= -lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= -modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw= -modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0= -modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw= -modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY= -modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk= -modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM= modernc.org/libc v1.21.5 h1:xBkU9fnHV+hvZuPSRszN0AXDG4M7nwPLwTWwkYcvLCI= modernc.org/libc v1.21.5/go.mod h1:przBsL5RDOZajTVslkugzLBj1evTue36jEomFQOoYuI= modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/memory v1.4.0 h1:crykUfNSnMAXaOJnnxcSzbUGMqkLWjklJKkBK2nwZwk= modernc.org/memory v1.4.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= -modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= -modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= -modernc.org/sqlite v1.20.0 h1:80zmD3BGkm8BZ5fUi/4lwJQHiO3GXgIUvZRXpoIfROY= -modernc.org/sqlite v1.20.0/go.mod h1:EsYz8rfOvLCiYTy5ZFsOYzoCcRMu98YYkwAcCw5YIYw= -modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= -modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= -modernc.org/tcl v1.15.0 h1:oY+JeD11qVVSgVvodMJsu7Edf8tr5E/7tuhF5cNYz34= -modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg= -modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= -modernc.org/z v1.7.0 h1:xkDw/KepgEjeizO2sNco+hqYkU12taxQFqPEmgm1GWE= diff --git a/openapi_user.go b/openapi_user.go index 25b5d51..fcdb1b9 100644 --- a/openapi_user.go +++ b/openapi_user.go @@ -17,6 +17,11 @@ func (u *User) At() string { return "<@!" + u.ID + ">" } +// AtMe 返回 <@!bot.ready.User.ID> +func (bot *Bot) AtMe() string { + return "<@!" + bot.ready.User.ID + ">" +} + // GetMyInfo 获取当前用户(机器人)详情 // // https://bot.q.qq.com/wiki/develop/api/openapi/user/me.html diff --git a/rule.go b/rule.go index c1db820..5c5f95c 100644 --- a/rule.go +++ b/rule.go @@ -43,23 +43,23 @@ func init() { switch ctx.State["command"] { case "响应", "response": if m.CanResponse(int64(grp)) { - msg = ctx.Caller.ready.User.Username + "已经在工作了哦~" + msg = ctx.GetReady().User.Username + "已经在工作了哦~" break } err := m.Response(int64(grp)) if err == nil { - msg = ctx.Caller.ready.User.Username + "将开始在此工作啦~" + msg = ctx.GetReady().User.Username + "将开始在此工作啦~" } else { msg = "ERROR: " + err.Error() } case "沉默", "silence": if !m.CanResponse(int64(grp)) { - msg = ctx.Caller.ready.User.Username + "已经在休息了哦~" + msg = ctx.GetReady().User.Username + "已经在休息了哦~" break } err := m.Silence(int64(grp)) if err == nil { - msg = ctx.Caller.ready.User.Username + "将开始休息啦~" + msg = ctx.GetReady().User.Username + "将开始休息啦~" } else { msg = "ERROR: " + err.Error() } @@ -78,14 +78,14 @@ func init() { case strings.Contains(cmd, "响应") || strings.Contains(cmd, "response"): err := m.Response(0) if err == nil { - msg = ctx.Caller.ready.User.Username + "将开始在此工作啦~" + msg = ctx.GetReady().User.Username + "将开始在此工作啦~" } else { msg = "ERROR: " + err.Error() } case strings.Contains(cmd, "沉默") || strings.Contains(cmd, "silence"): err := m.Silence(0) if err == nil { - msg = ctx.Caller.ready.User.Username + "将开始休息啦~" + msg = ctx.GetReady().User.Username + "将开始休息啦~" } else { msg = "ERROR: " + err.Error() } @@ -188,7 +188,7 @@ func init() { service.Permit(uid, int64(grp)) msg += "\n+ 已允许" + usr } else { - member, err := ctx.Caller.GetGuildMemberOf(ctx.Message.GuildID, usr) + member, err := ctx.GetGuildMemberOf(ctx.Message.GuildID, usr) if err == nil && !member.Pending { service.Permit(uid, int64(grp)) msg += "\n+ 已允许" + usr @@ -206,7 +206,7 @@ func init() { service.Ban(uid, int64(grp)) msg += "\n- 已禁止" + usr } else { - member, err := ctx.Caller.GetGuildMemberOf(ctx.Message.GuildID, usr) + member, err := ctx.GetGuildMemberOf(ctx.Message.GuildID, usr) if err == nil && !member.Pending { service.Ban(uid, int64(grp)) msg += "\n- 已禁止" + usr diff --git a/rules.go b/rules.go index d13353b..9b061c2 100644 --- a/rules.go +++ b/rules.go @@ -321,7 +321,7 @@ func SuperUserPermission(ctx *Ctx) bool { if msg.Author == nil { // 确保无空 return false } - for _, su := range ctx.Caller.SuperUsers { + for _, su := range ctx.caller.SuperUsers { if su == msg.Author.ID { return true } @@ -431,7 +431,7 @@ func MustProvidePhoto(onmessage string, needphohint, failhint string) Rule { } // 没有图片就索取 if needphohint != "" { - _, err := ctx.Caller.PostMessageToChannel(msg.ChannelID, &MessagePost{ + _, err := ctx.PostMessageToChannel(msg.ChannelID, &MessagePost{ Content: needphohint, MessageReference: &MessageReference{MessageID: msg.ID}, ReplyMessageID: msg.ID,