mirror of
https://github.com/KimigaiiWuyi/GenshinUID.git
synced 2025-05-12 06:55:58 +08:00
✨ 支持向adapter.discord
发送按钮
This commit is contained in:
parent
1c3567d899
commit
81b524a4dc
@ -167,6 +167,36 @@ async def get_notice_message(bot: Bot, ev: Event):
|
||||
else:
|
||||
logger.debug('[gsuid] 不支持该 Telegram 事件...')
|
||||
return
|
||||
elif bot.adapter.get_name() == 'Discord':
|
||||
from nonebot.adapters.discord.api import ChannelType
|
||||
from nonebot.adapters.discord import MessageComponentInteractionEvent
|
||||
|
||||
sender = {}
|
||||
if isinstance(ev, MessageComponentInteractionEvent):
|
||||
user_type = (
|
||||
'direct'
|
||||
if ev.channel and ev.channel.type == ChannelType.DM
|
||||
else 'group'
|
||||
)
|
||||
msg_id = str(ev.id)
|
||||
group_id = str(ev.channel_id)
|
||||
message = [Message('text', ev.data.custom_id)]
|
||||
if user_type == 'direct':
|
||||
nickname = ev.user.username # type: ignore
|
||||
avatar = ev.user.avatar # type: ignore
|
||||
user_id = str(ev.user.id) # type: ignore
|
||||
else:
|
||||
nickname = ev.member.user.username # type: ignore
|
||||
avatar = ev.member.user.avatar # type: ignore
|
||||
user_id = str(ev.member.user.id) # type: ignore
|
||||
sender = {
|
||||
'nickname': nickname,
|
||||
'avatar': 'https://cdn.discordapp.com/avatars/'
|
||||
f'{user_id}/{avatar}',
|
||||
}
|
||||
else:
|
||||
logger.debug('[gsuid] 不支持该 Discord 事件...')
|
||||
return
|
||||
else:
|
||||
return
|
||||
|
||||
@ -508,9 +538,20 @@ async def get_all_message(bot: Bot, ev: Event):
|
||||
user_type = 'group'
|
||||
msg_id = str(ev.message_id)
|
||||
group_id = str(int(ev.channel_id))
|
||||
sender = {
|
||||
'nickname': ev.author.username,
|
||||
'avatar': 'https://cdn.discordapp.com/avatars/'
|
||||
f'{user_id}/{ev.author.avatar}',
|
||||
}
|
||||
elif isinstance(ev, DirectMessageCreateEvent):
|
||||
msg_id = str(ev.message_id)
|
||||
user_type = 'direct'
|
||||
group_id = str(int(ev.channel_id))
|
||||
sender = {
|
||||
'nickname': ev.author.username,
|
||||
'avatar': 'https://cdn.discordapp.com/avatars/'
|
||||
f'{user_id}/{ev.author.avatar}',
|
||||
}
|
||||
else:
|
||||
logger.debug('[gsuid] 不支持该 Discord 事件...')
|
||||
return
|
||||
|
@ -290,6 +290,19 @@ class GsClient:
|
||||
msg.target_type,
|
||||
group_id,
|
||||
)
|
||||
elif msg.bot_id == 'discord':
|
||||
await discord_send(
|
||||
bot,
|
||||
content,
|
||||
image,
|
||||
node,
|
||||
at_list,
|
||||
markdown,
|
||||
buttons,
|
||||
msg.target_id,
|
||||
msg.target_type,
|
||||
group_id,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
except RuntimeError as e:
|
||||
@ -460,6 +473,16 @@ def _tg_kb(button: Dict):
|
||||
)
|
||||
|
||||
|
||||
def _dc_kb(button: Dict):
|
||||
from nonebot.adapters.discord.api import Button, ButtonStyle
|
||||
|
||||
return Button(
|
||||
label=button['text'],
|
||||
custom_id=button['data'],
|
||||
style=ButtonStyle.Primary,
|
||||
)
|
||||
|
||||
|
||||
async def villa_send(
|
||||
bot: Bot,
|
||||
content: Optional[str],
|
||||
@ -733,14 +756,15 @@ async def discord_send(
|
||||
buttons: Optional[Union[List[Dict], List[List[Dict]]]],
|
||||
target_id: Optional[str],
|
||||
target_type: Optional[str],
|
||||
group_id: Optional[str],
|
||||
):
|
||||
from nonebot.adapters.discord.message import parse_message
|
||||
from nonebot.adapters.discord.api import ActionRow
|
||||
from nonebot.adapters.discord import Bot, Message, MessageSegment
|
||||
|
||||
assert isinstance(bot, Bot)
|
||||
|
||||
async def _send(content: Optional[str], image: Optional[str]):
|
||||
if target_id:
|
||||
if group_id:
|
||||
message = Message()
|
||||
if image:
|
||||
img_bytes = base64.b64decode(image.replace('base64://', ''))
|
||||
@ -757,15 +781,33 @@ async def discord_send(
|
||||
if markdown:
|
||||
logger.warning('[gscore] discord暂不支持发送markdown消息')
|
||||
if buttons:
|
||||
logger.warning('[gscore] discord暂不支持发送markdown消息')
|
||||
bt = []
|
||||
for button in buttons:
|
||||
if isinstance(button, Dict):
|
||||
bt.append(_dc_kb(button))
|
||||
if len(bt) >= 2:
|
||||
message.append(
|
||||
MessageSegment.component(
|
||||
ActionRow(components=bt)
|
||||
)
|
||||
)
|
||||
bt = []
|
||||
if isinstance(button, List):
|
||||
_t = []
|
||||
for i in button:
|
||||
_t.append(_dc_kb(i))
|
||||
else:
|
||||
message.append(
|
||||
MessageSegment.component(
|
||||
ActionRow(components=_t)
|
||||
)
|
||||
)
|
||||
_t = []
|
||||
|
||||
message_data = parse_message(message)
|
||||
await bot.create_message(
|
||||
channel_id=int(target_id),
|
||||
nonce=None,
|
||||
tts=False,
|
||||
allowed_mentions=None,
|
||||
**message_data,
|
||||
await bot.call_api('trigger_typing_indicator', channel_id=group_id)
|
||||
await bot.send_to(
|
||||
channel_id=int(group_id),
|
||||
message=message,
|
||||
)
|
||||
|
||||
if node:
|
||||
|
Loading…
x
Reference in New Issue
Block a user