-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
165 lines (126 loc) · 5.35 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from discord.ext import commands
from discord_slash import SlashCommand, cog_ext
from audio import Audio
import discord
import json
import asyncio
import re
import os
# ctx為context的縮寫
def makeNicknameSorter(nickname):
"""
make nickname shorter.
RULE:Chinese characters > English characters > numbers > others
remove useless char in nickname with RULE
"""
if re.compile(u'[\u4E00-\u9FFF]+', re.I).findall(nickname):
newNickName = re.compile(u'[\u4E00-\u9FFF]+', re.I).findall(nickname)
print(newNickName)
print(nickname + ' short to ' + newNickName[-1])
return newNickName[-1][-3:]
if re.compile(r'[a-z]|[A-Z]+', re.I).findall(nickname):
newNickName = re.compile(r'[a-z]|[A-Z]+', re.I).findall(nickname)
print(nickname + ' short to ' + ''.join(newNickName))
print(newNickName)
return ''.join(newNickName)
if re.compile(r'[0-9]+', re.I).findall(nickname):
newNickName = re.compile(r'[0-9]+', re.I).findall(nickname)
print(nickname + ' short to ' + ''.join(newNickName))
print(newNickName)
return ''.join(newNickName)[0:3]
if re.compile(r'[^a-zA-Z0-9]+', re.I).findall(nickname):
newNickName = re.compile(r'[^a-zA-Z0-9]+', re.I).findall(nickname)
print(nickname + ' short to ' + ''.join(newNickName))
print(newNickName)
return ''.join(newNickName)[0:2]
return nickname
class VoiceBot(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.messageList = {}
self.ctx = {}
self.languageList = {}
async def playAudio(self, channel):
for message in self.messageList[channel]:
print(message, 'task generating...')
Audio(message, channel, self.languageList[channel])
print(message, 'task start talking...')
self.ctx[channel].voice_client.play(
discord.FFmpegPCMAudio(source=f'./audio/{channel}.mp3'))
while self.ctx[channel].voice_client.is_playing():
await asyncio.sleep(0.1)
await asyncio.sleep(0.5)
print(message, 'task finished')
self.messageList[channel] = []
@cog_ext.cog_slash(name='join')
async def join(self, ctx):
"""Joins a voice channel"""
if ctx.author.voice is None:
return await ctx.send("你不在頻道裡歐", delete_after=2.)
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()
channel = str(ctx.author.voice.channel.id)
self.ctx[channel] = ctx
self.messageList[channel] = []
self.languageList[channel] = 'zh'
print('connected to:', channel)
await ctx.author.voice.channel.connect()
return await ctx.send("哈哈,4我啦。", delete_after=2.)
@cog_ext.cog_slash(name='leave')
async def leave(self, ctx):
"""leaves a voice channel"""
if ctx.voice_client is None:
return await ctx.send("蛤?", delete_after=2.)
await ctx.send("債眷。", delete_after=2.)
return await ctx.voice_client.disconnect()
@cog_ext.cog_slash(name='talk')
async def talk(self, ctx, message):
"""plays the audio from message"""
if ctx.voice_client is None:
return await ctx.send('我沒進頻道歐', delete_after=2.)
if ctx.author.voice is None:
return await ctx.send("你不在頻道裡歐", delete_after=2.)
fullMessage = message + ', from ' + makeNicknameSorter(ctx.author.display_name)
print(fullMessage, 'add to queue')
channel = str(ctx.author.voice.channel.id)
if len(self.messageList[channel]) != 0:
self.messageList[channel].append(fullMessage)
else:
self.messageList[channel].append(fullMessage)
asyncio.ensure_future(self.playAudio(channel))
return await ctx.send(fullMessage + ' - 已加入列隊', delete_after=2.)
@cog_ext.cog_slash(name='language')
async def changeLanguage(self, ctx, language):
"""
Support language : en (English), fr (French), zh (Mandarin), pt (Portuguese), es (Spanish)
"""
if ctx.voice_client is None:
return await ctx.send('我沒進頻道歐', delete_after=2.)
if ctx.author.voice is None:
return await ctx.send("你不在頻道裡歐", delete_after=2.)
channel = str(ctx.author.voice.channel.id)
self.languageList[channel] = language
try:
Audio('test', channel, self.languageList[channel])
return await ctx.send('Change language: ' + language, delete_after=2.)
except ValueError:
self.languageList[channel] = 'zh'
return await ctx.send('Language not supported: ' + language, delete_after=2.)
def main():
try:
bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"),
description='py')
slash = SlashCommand(bot, sync_commands=True, sync_on_cog_reload=True)
@bot.event
async def on_ready():
print('Logged in as {0} ({0.id})'.format(bot.user))
vb = VoiceBot(bot)
bot.add_cog(vb)
with open('config.json', 'r') as f:
token = json.load(f)
bot.run(token['token'])
except RuntimeError:
main()
if __name__ == '__main__':
os.mkdir("audio")
main()