├── 3dengine.py ├── BrainF_ckInterpreter ├── BrainF_ckInterpreter.c └── HelloWorld.bf ├── FaceGen ├── tutorial.pyw └── tutorial1.tflite ├── LICENSE ├── README.md ├── Raytracer.cpp ├── rkcodingmusic.py ├── rkcodingmusicbotfinal.py ├── rkcodingmusicbotloop.py └── rkcodingmusicqueue.py /3dengine.py: -------------------------------------------------------------------------------- 1 | from math import sin, cos 2 | import pygame 3 | 4 | class Window: 5 | def __init__(self, width, height, title): 6 | self.width = width 7 | self.height = height 8 | self.title = title 9 | 10 | self.screen = pygame.display.set_mode((self.width, self.height)) 11 | pygame.display.set_caption(self.title) 12 | 13 | def key_check(self, key): 14 | keys = pygame.key.get_pressed() 15 | 16 | if (keys[key]): 17 | return True 18 | 19 | def update(self): 20 | for event in pygame.event.get(): 21 | if event.type == pygame.QUIT: 22 | pygame.quit() 23 | exit() 24 | 25 | pygame.display.update() 26 | 27 | class Mesh: 28 | def __init__(self, verts, edges, x, y, z, rotX, rotY, rotZ, scale): 29 | self.verts = verts 30 | self.edges = edges 31 | self.x = x 32 | self.y = y 33 | self.z = z 34 | self.rotX = rotX 35 | self.rotY = rotY 36 | self.rotZ = rotZ 37 | self.scale = scale 38 | 39 | def project_and_rotate(self, x, y, z): 40 | px = (((x * cos(self.rotZ) - sin(self.rotZ) * y) * cos(self.rotY) - z * sin(self.rotY)) * (200 / ((((z * cos(self.rotY) + (x * cos(self.rotZ) - sin(self.rotZ) * y) * sin(self.rotY)) * cos(self.rotX) + (y * cos(self.rotZ) + x * sin(self.rotZ)) * sin(self.rotX)) + 5) + self.z))) * self.scale + self.x 41 | py = (((y * cos(self.rotZ) + x * sin(self.rotZ)) * cos(self.rotX) - (z * cos(self.rotY) + (x * cos(self.rotZ) - sin(self.rotZ) * y) * sin(self.rotY)) * sin(self.rotX)) * (200 / ((((z * cos(self.rotY) + (x * cos(self.rotZ) - sin(self.rotZ) * y) * sin(self.rotY)) * cos(self.rotX) + (y * cos(self.rotZ) + x * sin(self.rotZ)) * sin(self.rotX)) + 5) + self.z))) * self.scale + self.y 42 | 43 | return (int(px), int(py)) 44 | 45 | def render(self, window): 46 | window.screen.fill((0, 0, 0)) 47 | 48 | for vert in self.verts: 49 | point = self.project_and_rotate(vert[0], vert[1], vert[2]) 50 | 51 | pygame.draw.circle(window.screen, (0, 255, 0), point, 6) 52 | 53 | for edge in self.edges: 54 | point1 = self.project_and_rotate(self.verts[edge[0]][0], self.verts[edge[0]][1], self.verts[edge[0]][2]) 55 | point2 = self.project_and_rotate(self.verts[edge[1]][0], self.verts[edge[1]][1], self.verts[edge[1]][2]) 56 | 57 | pygame.draw.line(window.screen, (0, 255, 0), point1, point2, 5) 58 | 59 | window = Window(500, 500, "Cube") 60 | cube = Mesh([(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, -1), (0, 1, -1), (1, 1, -1), (1, 0, -1)], [(0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7)], 250, 250, 5, 0, 0, 0, 5) 61 | 62 | while True: 63 | cube.rotX += 0.001 64 | cube.rotY += 0.01 65 | cube.rotZ += 0.001 66 | 67 | cube.render(window) 68 | window.update() -------------------------------------------------------------------------------- /BrainF_ckInterpreter/BrainF_ckInterpreter.c: -------------------------------------------------------------------------------- 1 | //Author: RK Coding 2 | //Description: A simple BrainF*ck Intepreter written in C 3 | //License: Contact Me 4 | 5 | #include 6 | 7 | #pragma warning(disable:4996) 8 | 9 | char* load_file(char const* path) { 10 | char* buffer = 0; 11 | long length = 0; 12 | FILE* f = fopen(path, "r"); //Open file in read mode 13 | 14 | fseek(f, 0, SEEK_END); 15 | length = ftell(f); 16 | fseek(f, 0, SEEK_SET); 17 | buffer = (char*)malloc((length + 1) * sizeof(char)); 18 | if (buffer) 19 | fread(buffer, sizeof(char), length, f); 20 | 21 | fclose(f); 22 | buffer[length] = '\0'; 23 | 24 | return buffer; 25 | } 26 | 27 | int main(int argc, char* argv[]) { 28 | //Create the pointer and array of cells 29 | int ptr = 0; 30 | int mem[1024]; 31 | 32 | for (int i = 0; i < 1024; ++i) //Set all values to zero 33 | mem[i] = 0; 34 | 35 | //Write file given at command line int char* buffer 36 | char* source = load_file(argv[1]); 37 | int char_num = 0; 38 | 39 | char c = source[char_num]; 40 | 41 | while (c != '\0') { //Parse characters 42 | c = source[char_num]; 43 | 44 | //Movement 45 | if (c == '>') { 46 | if (ptr != 1024) //Check if in bounds 47 | ++ptr; 48 | } 49 | 50 | else if (c == '<') { 51 | if (ptr != 0) //Check if in bounds 52 | --ptr; 53 | } 54 | 55 | //Modify 56 | else if (c == '+') { 57 | ++mem[ptr]; 58 | 59 | if (mem[ptr] > 255) // Loop over to 0 if greater than 255 60 | mem[ptr] = 0; 61 | } 62 | 63 | else if (c == '-') { 64 | --mem[ptr]; 65 | 66 | if (mem[ptr] < 0) // Loop over to 255 if less than 0 67 | mem[ptr] = 255; 68 | } 69 | 70 | //Loop 71 | else if (c == '[') { 72 | if (mem[ptr] == 0) { //If cell is equal than zero 73 | int count_open = 0; 74 | ++char_num; 75 | 76 | while (c != '\0') { //Match closed bracker 77 | c = source[char_num]; 78 | 79 | if (c == ']' && count_open == 0) 80 | break; 81 | 82 | else if (c == '[') 83 | ++count_open; 84 | 85 | else if (c == ']') 86 | --count_open; 87 | 88 | ++char_num; 89 | } 90 | } 91 | } 92 | 93 | else if (c == ']') { 94 | if (mem[ptr] > 0) { //Cell is greater than zero 95 | int count_closed = 0; 96 | --char_num; 97 | 98 | while (c != '\0') { //Match open bracket 99 | if (char_num > 0) //Is index in bounds; 100 | c = source[char_num]; 101 | 102 | if (c == '[' && count_closed == 0) 103 | break; 104 | 105 | else if (c == ']') 106 | ++count_closed; 107 | 108 | else if (c == '[') 109 | --count_closed; 110 | 111 | --char_num; 112 | } 113 | } 114 | } 115 | 116 | //IO 117 | else if (c == '.') 118 | printf("%c", mem[ptr]); 119 | 120 | else if (c == ',') 121 | mem[ptr] = getchar(c); 122 | 123 | ++char_num; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /BrainF_ckInterpreter/HelloWorld.bf: -------------------------------------------------------------------------------- 1 | >++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<+ 2 | +.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>- 3 | ]<+. 4 | -------------------------------------------------------------------------------- /FaceGen/tutorial.pyw: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | 4 | interpreter = tf.lite.Interpreter(model_path="tutorial1.tflite") 5 | interpreter.allocate_tensors() 6 | 7 | def sample(): 8 | input_details = interpreter.get_input_details() 9 | output_details = interpreter.get_output_details() 10 | 11 | input_shape = input_details[0]['shape'] 12 | latent = (np.random.random_sample(input_shape) - 0.5) * 2.0 13 | input_data = np.array(latent, dtype=np.float32) 14 | interpreter.set_tensor(input_details[0]['index'], input_data) 15 | 16 | interpreter.invoke() 17 | 18 | result = interpreter.get_tensor(output_details[0]['index']) 19 | result = np.reshape(result, [256,256,3]) 20 | result = (result + 1.0) * 127.5 21 | result = pygame.surfarray.make_surface(result) 22 | result = pygame.transform.rotate(result, -90) 23 | return result 24 | 25 | import pygame 26 | pygame.init() 27 | display = pygame.display.set_mode((300, 300)) 28 | 29 | surface = sample() 30 | running = True 31 | 32 | while running: 33 | for event in pygame.event.get(): 34 | if event.type == pygame.QUIT: 35 | running = False 36 | 37 | if event.type == pygame.KEYDOWN: 38 | if event.key == pygame.K_SPACE: 39 | surface = sample() 40 | 41 | display.blit(surface, (0, 0)) 42 | pygame.display.update() 43 | 44 | pygame.quit() 45 | -------------------------------------------------------------------------------- /FaceGen/tutorial1.tflite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RK-Coding/Videos/64c12a7f67e8ce5d64c3a1c3f03910a05b33a61a/FaceGen/tutorial1.tflite -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Contact me! You can't use without my permission. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Videos 2 | This repo contains all the code for the videos on RK Codings' channel. 3 | -------------------------------------------------------------------------------- /Raytracer.cpp: -------------------------------------------------------------------------------- 1 | //Author: RK Coding 2 | //Description: A raytracer created for a video on the RK Coding Youtube Channel 3 | //License: Contact Me 4 | 5 | //Standard Libs 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | //SDL 13 | #include 14 | 15 | //Globals 16 | #define WIDTH 800 17 | #define HEIGHT 600 18 | 19 | #define SAMPLE_SIZE 32 20 | 21 | struct Vec3 // Vector3 structure 22 | { 23 | public: 24 | float x, y, z; 25 | 26 | Vec3(float x, float y, float z) : x(x), y(y), z(z) {} 27 | 28 | Vec3 operator + (Vec3 o) { return Vec3(x+o.x, y+o.y, z+o.z); } 29 | Vec3 operator - (Vec3 o) { return Vec3(x-o.x, y-o.y, z-o.z); } 30 | Vec3 operator * (float d) { return Vec3(x*d, y*d, z*d); } 31 | Vec3 operator / (float d) { return Vec3(x/d, y/d, z/d); } 32 | float DotProduct(Vec3 o) { return (x*o.x+y*o.y+z*o.z); } 33 | Vec3 Normalize() 34 | { 35 | float mag = sqrtf(x*x+y*y+z*z); 36 | return Vec3(x/mag, y/mag, z/mag); 37 | } 38 | Vec3 ToColor() 39 | { 40 | float r, g, b; 41 | 42 | r = x; 43 | g = y; 44 | b = z; 45 | 46 | if (x < 0.0f) { r = 0.0f; } 47 | if (x > 255.0f) { r = 255.0f; } 48 | if (y < 0.0f) { g = 0.0f; } 49 | if (y > 255.0f) { g = 255.0f; } 50 | if (z < 0.0f) { b = 0.0f; } 51 | if (z > 255.0f) { b = 255.0f; } 52 | 53 | return Vec3(r, g, b); 54 | } 55 | }; 56 | 57 | struct Light 58 | { 59 | Vec3 pos, color; 60 | 61 | Light(Vec3 pos, Vec3 color) : pos(pos), color(color) {} 62 | }; 63 | 64 | struct Ray 65 | { 66 | public: 67 | Vec3 origin, dir; 68 | 69 | Ray(Vec3 origin, Vec3 dir) : origin(origin), dir(dir) {} 70 | }; 71 | 72 | class Sphere 73 | { 74 | public: 75 | Vec3 center; 76 | float radius; 77 | Vec3 color; 78 | float ambient, diffuse, specular; 79 | 80 | Sphere(Vec3 center, float radius, Vec3 color, float ambient, float diffuse, float specular) : center(center), radius(radius), color(color), ambient(ambient), diffuse(diffuse), specular(specular) {} 81 | 82 | float Intersects(Ray ray) 83 | { 84 | Vec3 sphere_to_ray = ray.origin - center; 85 | float b = 2 * ray.dir.DotProduct(sphere_to_ray); 86 | float c = sphere_to_ray.DotProduct(sphere_to_ray) - radius * radius; 87 | 88 | float disc = b * b - 4 * c; 89 | 90 | if (disc >= 0) 91 | { 92 | float dist = (-b - sqrtf(disc)) / 2; 93 | 94 | if (dist > 0) 95 | { 96 | return dist; 97 | } 98 | } 99 | 100 | return -1; 101 | } 102 | 103 | Vec3 NormalAt(Vec3 hit_point) { return (hit_point-center).Normalize(); } 104 | }; 105 | 106 | class Window //Window Class 107 | { 108 | public: 109 | bool running = false; 110 | SDL_Event event; 111 | 112 | SDL_Window* window; 113 | SDL_Renderer* renderer; 114 | 115 | SDL_Texture* texture; 116 | Uint32* pixels; 117 | 118 | int width, height; 119 | 120 | Window(int width, int height, const char* title) : width(width), height(height) 121 | { 122 | running = true; 123 | 124 | window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0); 125 | renderer = SDL_CreateRenderer(window, -1, 0); 126 | 127 | texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, width, height); 128 | pixels = new Uint32[width * height]; 129 | } 130 | 131 | void Update() 132 | { 133 | SDL_UpdateTexture(texture, NULL, pixels, width * sizeof(Uint32)); 134 | 135 | SDL_WaitEvent(&event); 136 | 137 | switch (event.type) 138 | { 139 | case SDL_QUIT: 140 | running = false; 141 | break; 142 | } 143 | 144 | SDL_RenderClear(renderer); 145 | SDL_RenderCopy(renderer, texture, NULL, NULL); 146 | SDL_RenderPresent(renderer); 147 | } 148 | 149 | void Destroy() 150 | { 151 | delete[] pixels; 152 | SDL_DestroyWindow(window); 153 | SDL_DestroyRenderer(renderer); 154 | SDL_DestroyTexture(texture); 155 | SDL_Quit; 156 | } 157 | 158 | void SetPixel(int x, int y, Vec3 color_vec) 159 | { 160 | Vec3 to_color = color_vec.ToColor(); 161 | Uint32 color = ((int)to_color.x << 16) + ((int)to_color.y << 8) + ((int)to_color.z); 162 | pixels[y * width + x] = color; 163 | } 164 | }; 165 | 166 | int main(int argc, char *argv[]) 167 | { 168 | //Data 169 | SDL_Init(SDL_INIT_EVERYTHING); 170 | 171 | Window win(WIDTH, HEIGHT, "Raytracer by RK Coding"); 172 | std::vector objects = { 173 | //Walls 174 | Sphere(Vec3(-1100.0f, 300.0f, 1000.0f), 1300.0f, Vec3(255.0f, 0.0f, 0.0f), 0.5f, 0.5f, 0.0f), 175 | Sphere(Vec3(400.0f, -900.0f, 1500.0f), 1300.0f, Vec3(0.0f, 255.0f, 0.0f), 0.5f, 0.5f, 0.0f), 176 | Sphere(Vec3(400.0f, 300.0f, 2000.0f), 1300.0f, Vec3(40.0f, 40.0f, 40.0f), 0.5f, 0.5f, 0.0f), 177 | Sphere(Vec3(1900.0f, 300.0f, 1000.0f), 1300.0f, Vec3(0.0f, 0.0f, 255.0f), 0.5f, 0.5f, 0.0f), 178 | Sphere(Vec3(400.0f, 1500.0f, 1500.0f), 1300.0f, Vec3(0.0f, 255.0f, 0.0f), 0.5f, 0.5f, 0.0f), 179 | 180 | //Sphere 181 | Sphere(Vec3(250.0f, 300.0f, 150.0f), 120.0f, Vec3(255.0f, 0.0f, 0.0f), 0.5f, 0.5f, 0.0f) 182 | }; 183 | 184 | std::vector lights = { Light(Vec3(250.0f, 250.0f, -500.0f), Vec3(255.0f, 255.0f, 255.0f)) }; 185 | 186 | std::default_random_engine generator; 187 | std::uniform_real_distribution distribution(0.0, 1.0); 188 | 189 | for (int x = 0; x < win.width; x++) //Loop through every pixel 190 | { 191 | for (int y = 0; y < win.height; y++) 192 | { 193 | Vec3 pixel_color(0.0f, 0.0f, 0.0f); 194 | std::vector colors; 195 | 196 | for (int i = 0; i < SAMPLE_SIZE; i++) //Use 32 samples 197 | { 198 | float distance = 10000.0f; 199 | Sphere selected_object(Vec3(0.0f, 0.0f, 0.0f), 0.0f, Vec3(0.0f, 0.0f, 0.0f), 0.0f, 0.0f, 0.0f); 200 | bool object_hit = false; 201 | 202 | //Ray offset by uniform 203 | Ray ray(Vec3(x + distribution(generator), y + distribution(generator), 0.0f), Vec3(0.0f, 0.0f, 1.0f)); 204 | 205 | for (Sphere object : objects) //Test intersections 206 | { 207 | if (object.Intersects(ray) > 0) 208 | { 209 | if (object.Intersects(ray) < distance) 210 | { 211 | distance = object.Intersects(ray); 212 | selected_object = object; 213 | object_hit = true; 214 | } 215 | } 216 | } 217 | 218 | if (object_hit) 219 | { 220 | //Ambient 221 | Vec3 object_color = selected_object.color * selected_object.ambient; 222 | Vec3 hit_pos = ray.origin + ray.dir * distance; 223 | Vec3 hit_normal = selected_object.NormalAt(hit_pos); 224 | Vec3 to_cam = Vec3(0.0f, 0.0f, 0.0f) - hit_pos; 225 | 226 | for (Light light : lights) 227 | { 228 | //Diffuse 229 | object_color = object_color + (selected_object.color * selected_object.diffuse * std::max(hit_normal.DotProduct((light.pos - hit_pos).Normalize()), 0.0f)); 230 | 231 | //Specular 232 | Vec3 half_vec = ((light.pos - hit_pos).Normalize() + to_cam).Normalize(); 233 | object_color = object_color + (light.color * selected_object.specular * pow(std::max(hit_normal.DotProduct(half_vec), 0.0f), 50)); 234 | } 235 | 236 | //object_color = selected_object.color * selected_object.ambient / (distance * 0.02); //(RK Coding Shading Method!!, Thought of by me!!) 237 | colors.push_back(object_color); 238 | } 239 | } 240 | 241 | for (Vec3 color : colors) 242 | { 243 | pixel_color = pixel_color + color; 244 | } 245 | 246 | pixel_color = pixel_color / SAMPLE_SIZE; 247 | 248 | win.SetPixel(x, y, pixel_color); 249 | } 250 | } 251 | 252 | while (win.running) 253 | { 254 | win.Update(); 255 | } 256 | 257 | win.Destroy(); 258 | 259 | return 0; 260 | } -------------------------------------------------------------------------------- /rkcodingmusic.py: -------------------------------------------------------------------------------- 1 | import discord 2 | from discord.ext import commands, tasks 3 | from discord.voice_client import VoiceClient 4 | import youtube_dl 5 | 6 | from random import choice 7 | 8 | youtube_dl.utils.bug_reports_message = lambda: '' 9 | 10 | ytdl_format_options = { 11 | 'format': 'bestaudio/best', 12 | 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 13 | 'restrictfilenames': True, 14 | 'noplaylist': True, 15 | 'nocheckcertificate': True, 16 | 'ignoreerrors': False, 17 | 'logtostderr': False, 18 | 'quiet': True, 19 | 'no_warnings': True, 20 | 'default_search': 'auto', 21 | 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 22 | } 23 | 24 | ffmpeg_options = { 25 | 'options': '-vn' 26 | } 27 | 28 | ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 29 | 30 | class YTDLSource(discord.PCMVolumeTransformer): 31 | def __init__(self, source, *, data, volume=0.5): 32 | super().__init__(source, volume) 33 | 34 | self.data = data 35 | 36 | self.title = data.get('title') 37 | self.url = data.get('url') 38 | 39 | @classmethod 40 | async def from_url(cls, url, *, loop=None, stream=False): 41 | loop = loop or asyncio.get_event_loop() 42 | data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 43 | 44 | if 'entries' in data: 45 | # take first item from a playlist 46 | data = data['entries'][0] 47 | 48 | filename = data['url'] if stream else ytdl.prepare_filename(data) 49 | return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 50 | 51 | 52 | client = commands.Bot(command_prefix='?') 53 | 54 | status = ['Jamming out to music!', 'Eating!', 'Sleeping!'] 55 | 56 | @client.event 57 | async def on_ready(): 58 | change_status.start() 59 | print('Bot is online!') 60 | 61 | @client.event 62 | async def on_member_join(member): 63 | channel = discord.utils.get(member.guild.channels, name='general') 64 | await channel.send(f'Welcome {member.mention}! Ready to jam out? See `?help` command for details!') 65 | 66 | @client.command(name='ping', help='This command returns the latency') 67 | async def ping(ctx): 68 | await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms') 69 | 70 | @client.command(name='hello', help='This command returns a random welcome message') 71 | async def hello(ctx): 72 | responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**'] 73 | await ctx.send(choice(responses)) 74 | 75 | @client.command(name='die', help='This command returns a random last words') 76 | async def die(ctx): 77 | responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead'] 78 | await ctx.send(choice(responses)) 79 | 80 | @client.command(name='credits', help='This command returns the credits') 81 | async def credits(ctx): 82 | await ctx.send('Made by `RK Coding`') 83 | await ctx.send('Thanks to `DiamondSlasher` for coming up with the idea') 84 | await ctx.send('Thanks to `KingSticky` for helping with the `?die` and `?creditz` command') 85 | 86 | @client.command(name='creditz', help='This command returns the TRUE credits') 87 | async def creditz(ctx): 88 | await ctx.send('**No one but me, lozer!**') 89 | 90 | @client.command(name='play', help='This command plays music') 91 | async def play(ctx, url): 92 | if not ctx.message.author.voice: 93 | await ctx.send("You are not connected to a voice channel") 94 | return 95 | 96 | else: 97 | channel = ctx.message.author.voice.channel 98 | 99 | await channel.connect() 100 | 101 | server = ctx.message.guild 102 | voice_channel = server.voice_client 103 | 104 | async with ctx.typing(): 105 | player = await YTDLSource.from_url(url, loop=client.loop) 106 | voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None) 107 | 108 | await ctx.send('**Now playing:** {}'.format(player.title)) 109 | 110 | @client.command(name='stop', help='This command stops the music and makes the bot leave the voice channel') 111 | async def stop(ctx): 112 | voice_client = ctx.message.guild.voice_client 113 | await voice_client.disconnect() 114 | 115 | @tasks.loop(seconds=20) 116 | async def change_status(): 117 | await client.change_presence(activity=discord.Game(choice(status))) 118 | 119 | client.run('token') 120 | -------------------------------------------------------------------------------- /rkcodingmusicbotfinal.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import discord 3 | from discord.ext import commands, tasks 4 | from discord.voice_client import VoiceClient 5 | import youtube_dl 6 | 7 | from random import choice 8 | 9 | youtube_dl.utils.bug_reports_message = lambda: '' 10 | 11 | ytdl_format_options = { 12 | 'format': 'bestaudio/best', 13 | 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 14 | 'restrictfilenames': True, 15 | 'noplaylist': True, 16 | 'nocheckcertificate': True, 17 | 'ignoreerrors': False, 18 | 'logtostderr': False, 19 | 'quiet': True, 20 | 'no_warnings': True, 21 | 'default_search': 'auto', 22 | 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 23 | } 24 | 25 | ffmpeg_options = { 26 | 'options': '-vn' 27 | } 28 | 29 | ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 30 | 31 | class YTDLSource(discord.PCMVolumeTransformer): 32 | def __init__(self, source, *, data, volume=0.5): 33 | super().__init__(source, volume) 34 | 35 | self.data = data 36 | 37 | self.title = data.get('title') 38 | self.url = data.get('url') 39 | 40 | @classmethod 41 | async def from_url(cls, url, *, loop=None, stream=False): 42 | loop = loop or asyncio.get_event_loop() 43 | data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 44 | 45 | if 'entries' in data: 46 | # take first item from a playlist 47 | data = data['entries'][0] 48 | 49 | filename = data['url'] if stream else ytdl.prepare_filename(data) 50 | return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 51 | 52 | def is_connected(ctx): 53 | voice_client = ctx.message.guild.voice_client 54 | return voice_client and voice_client.is_connected() 55 | 56 | client = commands.Bot(command_prefix='?') 57 | 58 | status = ['Jamming out to music!', 'Eating!', 'Sleeping!'] 59 | queue = [] 60 | loop = False 61 | 62 | @client.event 63 | async def on_ready(): 64 | change_status.start() 65 | print('Bot is online!') 66 | 67 | @client.event 68 | async def on_member_join(member): 69 | channel = discord.utils.get(member.guild.channels, name='general') 70 | await channel.send(f'Welcome {member.mention}! Ready to jam out? See `?help` command for details!') 71 | 72 | @client.command(name='ping', help='This command returns the latency') 73 | async def ping(ctx): 74 | await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms') 75 | 76 | @client.command(name='hello', help='This command returns a random welcome message') 77 | async def hello(ctx): 78 | responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**'] 79 | await ctx.send(choice(responses)) 80 | 81 | @client.command(name='die', help='This command returns a random last words') 82 | async def die(ctx): 83 | responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead'] 84 | await ctx.send(choice(responses)) 85 | 86 | @client.command(name='credits', help='This command returns the credits') 87 | async def credits(ctx): 88 | await ctx.send('Made by `RK Coding`') 89 | await ctx.send('Thanks to `DiamondSlasher` for coming up with the idea') 90 | await ctx.send('Thanks to `KingSticky` for helping with the `?die` and `?creditz` command') 91 | 92 | @client.command(name='creditz', help='This command returns the TRUE credits') 93 | async def creditz(ctx): 94 | await ctx.send('**No one but me, lozer!**') 95 | 96 | @client.command(name='join', help='This command makes the bot join the voice channel') 97 | async def join(ctx): 98 | if not ctx.message.author.voice: 99 | await ctx.send("You are not connected to a voice channel") 100 | return 101 | 102 | else: 103 | channel = ctx.message.author.voice.channel 104 | 105 | await channel.connect() 106 | 107 | @client.command(name='leave', help='This command stops the music and makes the bot leave the voice channel') 108 | async def leave(ctx): 109 | voice_client = ctx.message.guild.voice_client 110 | await voice_client.disconnect() 111 | 112 | @client.command(name='loop', help='This command toggles loop mode') 113 | async def loop_(ctx): 114 | global loop 115 | 116 | if loop: 117 | await ctx.send('Loop mode is now `False!`') 118 | loop = False 119 | 120 | else: 121 | await ctx.send('Loop mode is now `True!`') 122 | loop = True 123 | 124 | @client.command(name='play', help='This command plays music') 125 | async def play(ctx): 126 | global queue 127 | 128 | if not ctx.message.author.voice: 129 | await ctx.send("You are not connected to a voice channel") 130 | return 131 | 132 | elif len(queue) == 0: 133 | await ctx.send('Nothing in your queue! Use `?queue` to add a song!') 134 | 135 | else: 136 | try: 137 | channel = ctx.message.author.voice.channel 138 | await channel.connect() 139 | except: 140 | pass 141 | 142 | server = ctx.message.guild 143 | voice_channel = server.voice_client 144 | while queue: 145 | try: 146 | while voice_channel.is_playing() or voice_channel.is_paused(): 147 | await asyncio.sleep(2) 148 | pass 149 | 150 | except AttributeError: 151 | pass 152 | 153 | try: 154 | async with ctx.typing(): 155 | player = await YTDLSource.from_url(queue[0], loop=client.loop) 156 | voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None) 157 | 158 | if loop: 159 | queue.append(queue[0]) 160 | 161 | del(queue[0]) 162 | 163 | await ctx.send('**Now playing:** {}'.format(player.title)) 164 | 165 | except: 166 | break 167 | 168 | @client.command(name='volume', help='This command changes the bots volume') 169 | async def volume(ctx, volume: int): 170 | if ctx.voice_client is None: 171 | return await ctx.send("Not connected to a voice channel.") 172 | 173 | ctx.voice_client.source.volume = volume / 100 174 | await ctx.send(f"Changed volume to {volume}%") 175 | 176 | @client.command(name='pause', help='This command pauses the song') 177 | async def pause(ctx): 178 | server = ctx.message.guild 179 | voice_channel = server.voice_client 180 | 181 | voice_channel.pause() 182 | 183 | @client.command(name='resume', help='This command resumes the song!') 184 | async def resume(ctx): 185 | server = ctx.message.guild 186 | voice_channel = server.voice_client 187 | 188 | voice_channel.resume() 189 | 190 | @client.command(name='stop', help='This command stops the song!') 191 | async def stop(ctx): 192 | server = ctx.message.guild 193 | voice_channel = server.voice_client 194 | 195 | voice_channel.stop() 196 | 197 | @client.command(name='queue') 198 | async def queue_(ctx, *, url): 199 | global queue 200 | 201 | queue.append(url) 202 | await ctx.send(f'`{url}` added to queue!') 203 | 204 | @client.command(name='remove') 205 | async def remove(ctx, number): 206 | global queue 207 | 208 | try: 209 | del(queue[int(number)]) 210 | await ctx.send(f'Your queue is now `{queue}!`') 211 | 212 | except: 213 | await ctx.send('Your queue is either **empty** or the index is **out of range**') 214 | 215 | @client.command(name='view', help='This command shows the queue') 216 | async def view(ctx): 217 | await ctx.send(f'Your queue is now `{queue}!`') 218 | 219 | @tasks.loop(seconds=20) 220 | async def change_status(): 221 | await client.change_presence(activity=discord.Game(choice(status))) 222 | 223 | client.run('token') 224 | -------------------------------------------------------------------------------- /rkcodingmusicbotloop.py: -------------------------------------------------------------------------------- 1 | import discord 2 | from discord.ext import commands, tasks 3 | from discord.voice_client import VoiceClient 4 | import youtube_dl 5 | 6 | from random import choice 7 | 8 | youtube_dl.utils.bug_reports_message = lambda: '' 9 | 10 | ytdl_format_options = { 11 | 'format': 'bestaudio/best', 12 | 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 13 | 'restrictfilenames': True, 14 | 'noplaylist': True, 15 | 'nocheckcertificate': True, 16 | 'ignoreerrors': False, 17 | 'logtostderr': False, 18 | 'quiet': True, 19 | 'no_warnings': True, 20 | 'default_search': 'auto', 21 | 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 22 | } 23 | 24 | ffmpeg_options = { 25 | 'options': '-vn' 26 | } 27 | 28 | ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 29 | 30 | class YTDLSource(discord.PCMVolumeTransformer): 31 | def __init__(self, source, *, data, volume=0.5): 32 | super().__init__(source, volume) 33 | 34 | self.data = data 35 | 36 | self.title = data.get('title') 37 | self.url = data.get('url') 38 | 39 | @classmethod 40 | async def from_url(cls, url, *, loop=None, stream=False): 41 | loop = loop or asyncio.get_event_loop() 42 | data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 43 | 44 | if 'entries' in data: 45 | # take first item from a playlist 46 | data = data['entries'][0] 47 | 48 | filename = data['url'] if stream else ytdl.prepare_filename(data) 49 | return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 50 | 51 | def is_connected(ctx): 52 | voice_client = ctx.message.guild.voice_client 53 | return voice_client and voice_client.is_connected() 54 | 55 | client = commands.Bot(command_prefix='?') 56 | 57 | status = ['Jamming out to music!', 'Eating!', 'Sleeping!'] 58 | queue = [] 59 | loop = False 60 | 61 | @client.event 62 | async def on_ready(): 63 | change_status.start() 64 | print('Bot is online!') 65 | 66 | @client.event 67 | async def on_member_join(member): 68 | channel = discord.utils.get(member.guild.channels, name='general') 69 | await channel.send(f'Welcome {member.mention}! Ready to jam out? See `?help` command for details!') 70 | 71 | @client.command(name='ping', help='This command returns the latency') 72 | async def ping(ctx): 73 | await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms') 74 | 75 | @client.command(name='hello', help='This command returns a random welcome message') 76 | async def hello(ctx): 77 | responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**'] 78 | await ctx.send(choice(responses)) 79 | 80 | @client.command(name='die', help='This command returns a random last words') 81 | async def die(ctx): 82 | responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead'] 83 | await ctx.send(choice(responses)) 84 | 85 | @client.command(name='credits', help='This command returns the credits') 86 | async def credits(ctx): 87 | await ctx.send('Made by `RK Coding`') 88 | await ctx.send('Thanks to `DiamondSlasher` for coming up with the idea') 89 | await ctx.send('Thanks to `KingSticky` for helping with the `?die` and `?creditz` command') 90 | 91 | @client.command(name='creditz', help='This command returns the TRUE credits') 92 | async def creditz(ctx): 93 | await ctx.send('**No one but me, lozer!**') 94 | 95 | @client.command(name='join', help='This command makes the bot join the voice channel') 96 | async def join(ctx): 97 | if not ctx.message.author.voice: 98 | await ctx.send("You are not connected to a voice channel") 99 | return 100 | 101 | else: 102 | channel = ctx.message.author.voice.channel 103 | 104 | await channel.connect() 105 | 106 | @client.command(name='leave', help='This command stops the music and makes the bot leave the voice channel') 107 | async def leave(ctx): 108 | voice_client = ctx.message.guild.voice_client 109 | await voice_client.disconnect() 110 | 111 | @client.command(name='loop', help='This command toggles loop mode') 112 | async def loop_(ctx): 113 | global loop 114 | 115 | if loop: 116 | await ctx.send('Loop mode is now `False!`') 117 | loop = False 118 | 119 | else: 120 | await ctx.send('Loop mode is now `True!`') 121 | loop = True 122 | 123 | @client.command(name='play', help='This command plays music') 124 | async def play(ctx): 125 | global queue 126 | 127 | if not ctx.message.author.voice: 128 | await ctx.send("You are not connected to a voice channel") 129 | return 130 | 131 | else: 132 | channel = ctx.message.author.voice.channel 133 | 134 | try: await channel.connect() 135 | except: pass 136 | 137 | server = ctx.message.guild 138 | voice_channel = server.voice_client 139 | 140 | try: 141 | async with ctx.typing(): 142 | player = await YTDLSource.from_url(queue[0], loop=client.loop) 143 | voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None) 144 | 145 | if loop: 146 | queue.append(queue[0]) 147 | 148 | del(queue[0]) 149 | 150 | await ctx.send('**Now playing:** {}'.format(player.title)) 151 | 152 | except: 153 | await ctx.send('Nothing in your queue! Use `?queue` to add a song!') 154 | 155 | @client.command(name='pause', help='This command pauses the song') 156 | async def pause(ctx): 157 | server = ctx.message.guild 158 | voice_channel = server.voice_client 159 | 160 | voice_channel.pause() 161 | 162 | @client.command(name='resume', help='This command resumes the song!') 163 | async def resume(ctx): 164 | server = ctx.message.guild 165 | voice_channel = server.voice_client 166 | 167 | voice_channel.resume() 168 | 169 | @client.command(name='stop', help='This command stops the song!') 170 | async def stop(ctx): 171 | server = ctx.message.guild 172 | voice_channel = server.voice_client 173 | 174 | voice_channel.stop() 175 | 176 | @client.command(name='queue') 177 | async def queue_(ctx, url): 178 | global queue 179 | 180 | queue.append(url) 181 | await ctx.send(f'`{url}` added to queue!') 182 | 183 | @client.command(name='remove') 184 | async def remove(ctx, number): 185 | global queue 186 | 187 | try: 188 | del(queue[int(number)]) 189 | await ctx.send(f'Your queue is now `{queue}!`') 190 | 191 | except: 192 | await ctx.send('Your queue is either **empty** or the index is **out of range**') 193 | 194 | @client.command(name='view', help='This command shows the queue') 195 | async def view(ctx): 196 | await ctx.send(f'Your queue is now `{queue}!`') 197 | 198 | @tasks.loop(seconds=20) 199 | async def change_status(): 200 | await client.change_presence(activity=discord.Game(choice(status))) 201 | 202 | client.run('token') 203 | -------------------------------------------------------------------------------- /rkcodingmusicqueue.py: -------------------------------------------------------------------------------- 1 | import discord 2 | from discord.ext import commands, tasks 3 | from discord.voice_client import VoiceClient 4 | import youtube_dl 5 | 6 | from random import choice 7 | 8 | youtube_dl.utils.bug_reports_message = lambda: '' 9 | 10 | ytdl_format_options = { 11 | 'format': 'bestaudio/best', 12 | 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 13 | 'restrictfilenames': True, 14 | 'noplaylist': True, 15 | 'nocheckcertificate': True, 16 | 'ignoreerrors': False, 17 | 'logtostderr': False, 18 | 'quiet': True, 19 | 'no_warnings': True, 20 | 'default_search': 'auto', 21 | 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 22 | } 23 | 24 | ffmpeg_options = { 25 | 'options': '-vn' 26 | } 27 | 28 | ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 29 | 30 | class YTDLSource(discord.PCMVolumeTransformer): 31 | def __init__(self, source, *, data, volume=0.5): 32 | super().__init__(source, volume) 33 | 34 | self.data = data 35 | 36 | self.title = data.get('title') 37 | self.url = data.get('url') 38 | 39 | @classmethod 40 | async def from_url(cls, url, *, loop=None, stream=False): 41 | loop = loop or asyncio.get_event_loop() 42 | data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 43 | 44 | if 'entries' in data: 45 | # take first item from a playlist 46 | data = data['entries'][0] 47 | 48 | filename = data['url'] if stream else ytdl.prepare_filename(data) 49 | return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 50 | 51 | 52 | client = commands.Bot(command_prefix='?') 53 | 54 | status = ['Jamming out to music!', 'Eating!', 'Sleeping!'] 55 | queue = [] 56 | 57 | @client.event 58 | async def on_ready(): 59 | change_status.start() 60 | print('Bot is online!') 61 | 62 | @client.event 63 | async def on_member_join(member): 64 | channel = discord.utils.get(member.guild.channels, name='general') 65 | await channel.send(f'Welcome {member.mention}! Ready to jam out? See `?help` command for details!') 66 | 67 | @client.command(name='ping', help='This command returns the latency') 68 | async def ping(ctx): 69 | await ctx.send(f'**Pong!** Latency: {round(client.latency * 1000)}ms') 70 | 71 | @client.command(name='hello', help='This command returns a random welcome message') 72 | async def hello(ctx): 73 | responses = ['***grumble*** Why did you wake me up?', 'Top of the morning to you lad!', 'Hello, how are you?', 'Hi', '**Wasssuup!**'] 74 | await ctx.send(choice(responses)) 75 | 76 | @client.command(name='die', help='This command returns a random last words') 77 | async def die(ctx): 78 | responses = ['why have you brought my short life to an end', 'i could have done so much more', 'i have a family, kill them instead'] 79 | await ctx.send(choice(responses)) 80 | 81 | @client.command(name='credits', help='This command returns the credits') 82 | async def credits(ctx): 83 | await ctx.send('Made by `RK Coding`') 84 | await ctx.send('Thanks to `DiamondSlasher` for coming up with the idea') 85 | await ctx.send('Thanks to `KingSticky` for helping with the `?die` and `?creditz` command') 86 | 87 | @client.command(name='creditz', help='This command returns the TRUE credits') 88 | async def creditz(ctx): 89 | await ctx.send('**No one but me, lozer!**') 90 | 91 | @client.command(name='join', help='This command makes the bot join the voice channel') 92 | async def join(ctx): 93 | if not ctx.message.author.voice: 94 | await ctx.send("You are not connected to a voice channel") 95 | return 96 | 97 | else: 98 | channel = ctx.message.author.voice.channel 99 | 100 | await channel.connect() 101 | 102 | @client.command(name='queue', help='This command adds a song to the queue') 103 | async def queue_(ctx, url): 104 | global queue 105 | 106 | queue.append(url) 107 | await ctx.send(f'`{url}` added to queue!') 108 | 109 | @client.command(name='remove', help='This command removes an item from the list') 110 | async def remove(ctx, number): 111 | global queue 112 | 113 | try: 114 | del(queue[int(number)]) 115 | await ctx.send(f'Your queue is now `{queue}!`') 116 | 117 | except: 118 | await ctx.send('Your queue is either **empty** or the index is **out of range**') 119 | 120 | @client.command(name='play', help='This command plays songs') 121 | async def play(ctx): 122 | global queue 123 | 124 | server = ctx.message.guild 125 | voice_channel = server.voice_client 126 | 127 | async with ctx.typing(): 128 | player = await YTDLSource.from_url(queue[0], loop=client.loop) 129 | voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None) 130 | 131 | await ctx.send('**Now playing:** {}'.format(player.title)) 132 | del(queue[0]) 133 | 134 | @client.command(name='pause', help='This command pauses the song') 135 | async def pause(ctx): 136 | server = ctx.message.guild 137 | voice_channel = server.voice_client 138 | 139 | voice_channel.pause() 140 | 141 | @client.command(name='resume', help='This command resumes the song!') 142 | async def resume(ctx): 143 | server = ctx.message.guild 144 | voice_channel = server.voice_client 145 | 146 | voice_channel.resume() 147 | 148 | @client.command(name='view', help='This command shows the queue') 149 | async def view(ctx): 150 | await ctx.send(f'Your queue is now `{queue}!`') 151 | 152 | @client.command(name='leave', help='This command stops makes the bot leave the voice channel') 153 | async def leave(ctx): 154 | voice_client = ctx.message.guild.voice_client 155 | await voice_client.disconnect() 156 | 157 | @client.command(name='stop', help='This command stops the song!') 158 | async def stop(ctx): 159 | server = ctx.message.guild 160 | voice_channel = server.voice_client 161 | 162 | voice_channel.stop() 163 | 164 | @tasks.loop(seconds=20) 165 | async def change_status(): 166 | await client.change_presence(activity=discord.Game(choice(status))) 167 | 168 | client.run('token') 169 | --------------------------------------------------------------------------------