问题已经通过抽出成函数来复用解决了,更多是对这个差异发生在哪里感到好奇,多谢回复
有个我所不理解的地方,下面是更多信息的补充和试验
代码如下:
ctx.command('test.command').action(async ({ session }) => {
console.log('test.command#action')
console.log(session)
})
ctx.on('bot-status-updated', async (bot) => {
if (bot.status === 1) {
for (const gid of cfg.retry) {
const event: Partial<Event> = {
channel: await bot.getChannel(gid),
guild: await bot.getGuild(gid),
user: await bot.getUser(bot.selfId),
member: await bot.getGuildMember(gid, bot.selfId),
type: 'guild-member-request',
}
console.log(event, '\n')
const session = bot.session(event)
console.log(session, '\n')
await session.send('debug#start').then(() => {
console.log('debug#start', '\n')
session.execute('test.command').then((ret) => {
console.log(ret)
console.log('debug#end')
})
})
}
}
})
本地机器上打印可见
{
channel: { id: '1019536708', name: 'IWS2000-DEBUG', type: 0 },
guild: { id: '1019536708', name: 'IWS2000-DEBUG' },
user: {
id: '3951441492',
name: 'IWS-2000',
userId: '3951441492',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=3951441492&spec=640',
username: 'IWS-2000'
},
member: {
user: {
id: '3951441492',
name: 'IWS-2000',
userId: '3951441492',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=3951441492&spec=640',
username: 'IWS-2000'
},
nick: 'iws-2000',
roles: [ 'owner' ]
},
type: 'guild-member-request'
}
Session {
id: 57,
sn: 57,
event: {
channel: { id: '1019536708', name: 'IWS2000-DEBUG', type: 0 },
guild: { id: '1019536708', name: 'IWS2000-DEBUG' },
user: {
id: '3951441492',
name: 'IWS-2000',
userId: '3951441492',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=3951441492&spec=640',
username: 'IWS-2000'
},
member: { user: [Object], nick: 'iws-2000', roles: [Array] },
type: 'guild-member-request',
selfId: '3951441492',
platform: 'onebot',
timestamp: 1774092556377
},
locales: [],
Symbol(cordis.tracker): { associate: 'session', property: 'ctx' }
}
debug#start
test.command#action
Session {
id: 57,
sn: 57,
event: {
channel: { id: '1019536708', name: 'IWS2000-DEBUG', type: 0 },
guild: { id: '1019536708', name: 'IWS2000-DEBUG' },
user: {
id: '3951441492',
name: 'IWS-2000',
userId: '3951441492',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=3951441492&spec=640',
username: 'IWS-2000'
},
member: { user: [Object], nick: 'iws-2000', roles: [Array] },
type: 'guild-member-request',
selfId: '3951441492',
platform: 'onebot',
timestamp: 1774092556377
},
locales: [],
guild: {
permissions: [],
locales: [],
platform: 'onebot',
id: '1019536708'
},
channel: {
permissions: [],
locales: [],
platform: 'onebot',
id: '1019536708'
},
user: { authority: 1, permissions: [], locales: [] },
scope: 'commands.test.command.messages',
Symbol(cordis.tracker): { associate: 'session', property: 'ctx' }
}
[]
debug#end
服务器上的打印
{
channel: { id: '468674513', name: 'GRAYGOO-DEBUG', type: 0 },
guild: { id: '468674513', name: 'GRAYGOO-DEBUG' },
user: {
id: '2817987433',
name: '小灰',
userId: '2817987433',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=2817987433&spec=640',
username: '小灰'
},
member: {
user: {
id: '2817987433',
name: '小灰',
userId: '2817987433',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=2817987433&spec=640',
username: '小灰'
},
nick: '',
roles: [ 'owner' ]
},
type: 'guild-member-request'
}
Session {
id: 24,
sn: 24,
event: {
channel: { id: '468674513', name: 'GRAYGOO-DEBUG', type: 0 },
guild: { id: '468674513', name: 'GRAYGOO-DEBUG' },
user: {
id: '2817987433',
name: '小灰',
userId: '2817987433',
avatar: 'http://q.qlogo.cn/headimg_dl?dst_uin=2817987433&spec=640',
username: '小灰'
},
member: { user: [Object], nick: '', roles: [Array] },
type: 'guild-member-request',
selfId: '2817987433',
platform: 'onebot',
timestamp: 1774092920882
},
locales: [],
Symbol(cordis.tracker): { associate: 'session', property: 'ctx' }
}
debug#start
[]
debug#end
环境方面两者均新建了一个独立的 koishi 实例,可见两者的 Session 对象都是几乎一致,但 session.execute() 不会解析执行传入的字符串
不过另一个测试佐证了手搓的 session 确实不稳定
ctx.command('test.debug').action(async ({ session }) => {
console.log('debug#start')
await session?.bot.session(session?.event).execute('test.command')
console.log('debug#end')
})
本地和服务器都 execute('test.command') 了命令
debug#start
test.command#action
debug#end
该读源码了