Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit ecfdb67

Browse files
committed
新增: VoiceMessage;
1 parent 976c8e9 commit ecfdb67

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#pragma once
2+
#ifndef mirai_cpp_defs_messages_voice_message_hpp_H_
3+
#define mirai_cpp_defs_messages_image_message_hpp_H_
4+
5+
#include "mirai/defs/message_interface.hpp"
6+
#include "mirai/defs/qq_types.hpp"
7+
8+
namespace Cyan
9+
{
10+
class VoiceMessage : public IMessage
11+
{
12+
public:
13+
VoiceMessage() {}
14+
VoiceMessage(const MiraiVoice& m)
15+
{
16+
voiceId_ = m.Id;
17+
url_ = m.Url;
18+
path_ = m.Path;
19+
}
20+
virtual const string& GetType() const override
21+
{
22+
return type_;
23+
}
24+
virtual bool operator==(const IMessage& m) const override
25+
{
26+
if (auto m_ptr = dynamic_cast<const VoiceMessage*>(&m))
27+
{
28+
// 用 voiceId 作为判断依据,必须有一个 voiceId 不为空
29+
if (!m_ptr->voiceId_.empty() || !this->voiceId_.empty())
30+
{
31+
return (m_ptr->voiceId_ == this->voiceId_);
32+
}
33+
// 如果 voiceId 都为空,那么用 url 再判断一下
34+
if (!m_ptr->url_.empty() || !this->url_.empty())
35+
{
36+
return (m_ptr->url_ == this->url_);
37+
}
38+
// 如果 url 都为空,那么用 path 再判断一下
39+
if (!m_ptr->path_.empty() || !this->path_.empty())
40+
{
41+
return (m_ptr->path_ == this->path_);
42+
}
43+
// 三个参数都为空,两个空的 VoiceMessage 当然是相等的:
44+
return true;
45+
}
46+
// 类型都不同,直接不相等:
47+
return false;
48+
}
49+
virtual bool operator!=(const IMessage& m) const override
50+
{
51+
return !(*this == m);
52+
}
53+
virtual bool Set(const json& json) override
54+
{
55+
if (json["type"].is_null() || json["type"].get<string>() != this->GetType())
56+
throw std::runtime_error("给定的json不正确");
57+
if (!json["voiceId"].is_null())
58+
voiceId_ = json["voiceId"].get<string>();
59+
if (!json["url"].is_null())
60+
url_ = json["url"].get<string>();
61+
if (!json["path"].is_null())
62+
path_ = json["path"].get<string>();
63+
return true;
64+
}
65+
virtual json ToJson() const override
66+
{
67+
return
68+
{
69+
{ "type", type_ },
70+
{ "voiceId", voiceId_ },
71+
{ "url", url_ },
72+
{ "path", path_ }
73+
};
74+
}
75+
virtual ~VoiceMessage() {}
76+
77+
MiraiVoice ToMiraiVoice() const
78+
{
79+
MiraiVoice tmp;
80+
tmp.Id = voiceId_;
81+
tmp.Url = url_;
82+
tmp.Path = path_;
83+
return tmp;
84+
}
85+
86+
private:
87+
string type_ = "Voice";
88+
protected:
89+
string voiceId_;
90+
string url_;
91+
string path_;
92+
};
93+
94+
}
95+
#endif
96+

0 commit comments

Comments
 (0)