-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
174 lines (134 loc) · 5.86 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
166
167
168
169
170
171
172
173
174
import discord
import responses
import asyncio
import os
import youtube_dl
import random
from dotenv import load_dotenv
from discord.ext import commands,tasks
async def send_message(message, user_message, is_private):
try:
response = responses.get_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
def run_discord_bot():
load_dotenv()
TOKEN =
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!',intents=intents)
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoteerrors': False,
'logtostderr': False,
'quiet':True,
'no_warnings':True,
'default_search': 'auto',
'source_address':'0.0.0.0'
}
ffmeg_options = {
'options': '-vn'
}
@bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.online, activity=discord.Game("공사장노동"))
print(f'{bot.user} is now running!')
@bot.event
async def on_message(message):
if message.author == bot.user:
return
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
print(f'{username} said: "{user_message}" ({channel})')
if user_message[0] == '?':
user_message = user_message[1:]
await send_message(message, user_message, is_private=True)
else:
await bot.process_commands(message)
await send_message(message, user_message, is_private=False)
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = ""
@classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda:ytdl.extract_info(url, download=not stream))
if 'entries' in data:
data = data['entries'][0]
filename = data['title'] if stream else ytdl.prepare_filename(data)
return filename
@bot.command(name='join',help='Tells the bot to join the voice channel')
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send("{}is not connected to a voice channel".format(ctx.message.author.name))
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
@bot.command(name='leave', help='To make the bot leave the voice channel')
async def leave(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_connected():
await voice_client.disconnect()
else:
await ctx.send("The bot is not connected to a voice channel.")
@bot.command(name='play_song', help='To play song')
async def play(ctx,url):
try :
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
filename = await YTDLSource.from_url(url, loop=bot.loop)
voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
await ctx.send('**Now playing:** {}'.format(filename))
except:
await ctx.send("The bot is not connected to a voice channel.")
@bot.command(name='pause', help='This command pauses the song')
async def pause(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_playing():
await voice_client.pause()
else:
await ctx.send("The bot is not playing anything at the moment.")
@bot.command(name='resume', help='Resumes the song')
async def resume(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_paused():
await voice_client.resume()
else:
await ctx.send("The bot was not playing anything before this. Use play_song command")
@bot.command(name='stop', help='Stops the song')
async def stop(ctx):
voice_client = ctx.message.guild.voice_client
if voice_client.is_playing():
await voice_client.stop()
else:
await ctx.send("The bot is not playing anything at the moment.")
@bot.command(name='재웅아노래불러줘', help='재웅이가 랩을 합니다')
async def 재웅아노래불러줘(ctx,url='https://www.youtube.com/watch?v=7eC5HMfzleQ'):
try :
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
filename = await YTDLSource.from_url(url='https://www.youtube.com/watch?v=7eC5HMfzleQ', loop=bot.loop)
voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg", source=filename))
await ctx.send('**재웅이가 부릅니다:** {}'.format(filename))
except:
await ctx.send("The bot is not connected to a voice channel.")
@bot.command(name='die', help='turn off the bot')
async def die(ctx):
await ctx.send("전 이세계를 이만 떠납니다 모두들 안녕")
await ctx.send("Please observe, however, that my Presence will remain incorrectly Online for about 110 seconds.")
await ctx.send("잘있어라 인생아")
await bot.close()
bot.run(TOKEN)