|
1
|
|
|
import discord |
|
2
|
|
|
from aiohttp import request |
|
3
|
|
|
from discord.ext import commands |
|
4
|
|
|
import database as db |
|
5
|
|
|
import variables as var |
|
6
|
|
|
from functions import get_prefix |
|
7
|
|
|
from ext.permissions import has_command_permission |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Chatbot(commands.Cog): |
|
11
|
|
|
def __init__(self, bot): |
|
12
|
|
|
self.bot = bot |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
async def cog_check(self, ctx): |
|
|
|
|
|
|
16
|
|
|
"""Simple check to see if this cog (plugin) is enabled""" |
|
17
|
|
|
guild_doc = await db.PLUGINS.find_one({"_id": ctx.guild.id}) |
|
18
|
|
|
|
|
19
|
|
|
if guild_doc.get("Chatbot"): |
|
20
|
|
|
return True |
|
21
|
|
|
|
|
22
|
|
|
else: |
|
23
|
|
|
await ctx.send( |
|
24
|
|
|
embed=discord.Embed( |
|
25
|
|
|
description=( |
|
26
|
|
|
f"{var.E_DISABLE} The Chatbot plugin is" |
|
27
|
|
|
" disabled in this server" |
|
28
|
|
|
), |
|
29
|
|
|
color=var.C_ORANGE |
|
30
|
|
|
) |
|
31
|
|
|
) |
|
32
|
|
|
|
|
33
|
|
|
@commands.command(name="setchatbot", aliases=["enablechatbot"]) |
|
34
|
|
|
@has_command_permission() |
|
35
|
|
|
async def set_chat_bot(self, ctx, channel: discord.TextChannel = None): |
|
36
|
|
|
if await db.CHATBOT.find_one({"_id": ctx.guild.id}) is None: |
|
37
|
|
|
await db.CHATBOT.insert_one( |
|
38
|
|
|
{ |
|
39
|
|
|
"_id": ctx.guild.id, |
|
40
|
|
|
"channels": [ctx.channel.id] |
|
41
|
|
|
} |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
if channel is None: |
|
45
|
|
|
channel = ctx.channel |
|
46
|
|
|
|
|
47
|
|
|
guild_doc = await db.CHATBOT.find_one({"_id": ctx.guild.id}) |
|
48
|
|
|
channel_list = guild_doc.get("channels") |
|
49
|
|
|
|
|
50
|
|
|
new_list = channel_list.copy() |
|
51
|
|
|
new_list.append(channel.id) |
|
52
|
|
|
new_data = { |
|
53
|
|
|
"$set": { |
|
54
|
|
|
"channels": new_list |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
await db.CHATBOT.update_one(guild_doc, new_data) |
|
59
|
|
|
await ctx.send(f"{var.E_ACCEPT} Enabled Chatbot for {channel.mention}") |
|
60
|
|
|
|
|
61
|
|
|
@commands.command(name="removechatbot", aliases=["disablechatbot"]) |
|
62
|
|
|
@has_command_permission() |
|
63
|
|
|
async def remove_chat_bot(self, ctx, channel: discord.TextChannel = None): |
|
64
|
|
|
if await db.CHATBOT.find_one({"_id": ctx.guild.id}) is None: |
|
65
|
|
|
await db.CHATBOT.insert_one( |
|
66
|
|
|
{ |
|
67
|
|
|
"_id": ctx.guild.id, |
|
68
|
|
|
"channels": [] |
|
69
|
|
|
} |
|
70
|
|
|
) |
|
71
|
|
|
|
|
72
|
|
|
if channel is None: |
|
73
|
|
|
channel = ctx.channel |
|
74
|
|
|
|
|
75
|
|
|
guild_doc = await db.CHATBOT.find_one({"_id": ctx.guild.id}) |
|
76
|
|
|
|
|
77
|
|
|
if channel.id in guild_doc.get("channels"): |
|
78
|
|
|
channel_list = guild_doc.get("channels") |
|
79
|
|
|
new_list = channel_list.copy() |
|
80
|
|
|
new_list.remove(channel.id) |
|
81
|
|
|
new_data = { |
|
82
|
|
|
"$set": { |
|
83
|
|
|
"channels": new_list |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
await db.CHATBOT.update_one(guild_doc, new_data) |
|
88
|
|
|
await ctx.send(f"Disabled Chatbot from {channel.mention}") |
|
89
|
|
|
|
|
90
|
|
|
else: |
|
91
|
|
|
await ctx.send("This channel is not a bot chatting channel") |
|
92
|
|
|
|
|
93
|
|
|
@commands.command(name="chatbotchannels") |
|
94
|
|
|
@has_command_permission() |
|
95
|
|
|
async def chat_bot_channels(self, ctx): |
|
96
|
|
|
guild_doc = await db.CHATBOT.find_one({"_id": ctx.guild.id}) |
|
97
|
|
|
embed = discord.Embed( |
|
98
|
|
|
title="All chatbot channels in this server", |
|
99
|
|
|
color=var.C_TEAL |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
if guild_doc is not None and guild_doc.get("channels") != []: |
|
103
|
|
|
for i in guild_doc.get("channels"): |
|
104
|
|
|
embed.add_field( |
|
105
|
|
|
name="** **", |
|
106
|
|
|
value=self.bot.get_channel(i).mention |
|
107
|
|
|
) |
|
108
|
|
|
|
|
109
|
|
|
await ctx.send(embed=embed) |
|
110
|
|
|
|
|
111
|
|
|
else: |
|
112
|
|
|
await ctx.send("This server does not have any chat bot channel") |
|
113
|
|
|
|
|
114
|
|
|
@commands.command(name="chatbotreport") |
|
115
|
|
|
@has_command_permission() |
|
116
|
|
|
async def chat_bot_report(self, ctx, *, desc): |
|
117
|
|
|
# Support server suggestions channel |
|
118
|
|
|
channel = self.bot.get_channel(843548616505294848) |
|
119
|
|
|
|
|
120
|
|
|
await channel.send( |
|
121
|
|
|
embed=discord.Embed( |
|
122
|
|
|
title="Chatbot suggestion", |
|
123
|
|
|
description=desc, |
|
124
|
|
|
color=var.C_MAIN |
|
125
|
|
|
).add_field( |
|
126
|
|
|
name="By", value=ctx.author |
|
127
|
|
|
).add_field( |
|
128
|
|
|
name="Guild ID", value=ctx.guild.id) |
|
129
|
|
|
) |
|
130
|
|
|
|
|
131
|
|
|
@commands.Cog.listener() |
|
132
|
|
|
async def on_message(self, message): |
|
133
|
|
|
if not message.guild: |
|
134
|
|
|
return |
|
135
|
|
|
|
|
136
|
|
|
guild_plugin_doc = await db.PLUGINS.find_one({"_id": message.guild.id}) |
|
137
|
|
|
ctx = await self.bot.get_context(message) |
|
138
|
|
|
|
|
139
|
|
|
if guild_plugin_doc["Chatbot"] and message.channel.id != 803308171577393172: |
|
140
|
|
|
guild_chatbot_doc = await db.CHATBOT.find_one( |
|
141
|
|
|
{"_id": message.guild.id} |
|
142
|
|
|
) |
|
143
|
|
|
|
|
144
|
|
|
def channels(): |
|
145
|
|
|
if ( |
|
146
|
|
|
guild_chatbot_doc is not None |
|
147
|
|
|
and guild_chatbot_doc.get("channels") != [] |
|
148
|
|
|
): |
|
149
|
|
|
channels = guild_chatbot_doc.get("channels") |
|
150
|
|
|
|
|
151
|
|
|
else: |
|
152
|
|
|
channels = [] |
|
153
|
|
|
|
|
154
|
|
|
return channels |
|
155
|
|
|
|
|
156
|
|
|
if ( |
|
157
|
|
|
self.bot.user in message.mentions |
|
158
|
|
|
and message.author.bot == False |
|
159
|
|
|
or message.channel.id in channels() |
|
160
|
|
|
and message.author.bot == False |
|
161
|
|
|
): |
|
162
|
|
|
|
|
163
|
|
|
content = message.content.replace("<@!843484459113775114>", "") |
|
164
|
|
|
async with request( |
|
165
|
|
|
"POST", |
|
166
|
|
|
f"https://axiol.up.railway.app/ai/chatbot", |
|
167
|
|
|
json={"content": content} |
|
168
|
|
|
) as response: |
|
169
|
|
|
res = await response.json() |
|
170
|
|
|
|
|
171
|
|
|
if res["response"] == "help": |
|
172
|
|
|
await ctx.invoke(self.bot.get_command('help')) |
|
173
|
|
|
|
|
174
|
|
|
elif res["tag"] == "prefix": |
|
175
|
|
|
await message.channel.send( |
|
176
|
|
|
res["response"].replace("~", await get_prefix(ctx))) |
|
177
|
|
|
|
|
178
|
|
|
else: |
|
179
|
|
|
await message.channel.send(res["response"]) |
|
180
|
|
|
|
|
181
|
|
|
elif self.bot.user.mention in message.mentions: |
|
182
|
|
|
await ctx.invoke(self.bot.get_command("help")) |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
def setup(bot): |
|
186
|
|
|
bot.add_cog(Chatbot(bot)) |
|
187
|
|
|
|