feat: add bot welcome message for new member
This commit is contained in:
30
cogs/fun.py
30
cogs/fun.py
@@ -2,6 +2,8 @@ import random
|
||||
import discord
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
from discord.ext import commands
|
||||
|
||||
class Fun(commands.Cog):
|
||||
@@ -9,7 +11,7 @@ class Fun(commands.Cog):
|
||||
|
||||
@commands.command()
|
||||
async def flip(self, ctx) -> None:
|
||||
"""Will flip a coin and return either heads or tails."""
|
||||
"""Flips a coin and return either heads or tails."""
|
||||
|
||||
coin = random.randint(0, 1)
|
||||
message = "heads!" if coin else "tails!"
|
||||
@@ -20,13 +22,13 @@ class Fun(commands.Cog):
|
||||
"""Returns the github repo information for a given repo name."""
|
||||
|
||||
if len(repo_name.strip()) == 0:
|
||||
await ctx.send("**Please provide a repo name. ex.** `gio101046/pyvis`")
|
||||
await ctx.send("**Please provide a repo name. ex.** `!github gio101046/pyvis`")
|
||||
return
|
||||
|
||||
http_response = requests.get(f"https://api.github.com/repos/{repo_name}")
|
||||
repo_info = json.loads(http_response.text)
|
||||
|
||||
if http_response.status_code == 404:
|
||||
if http_response.status_code == 404:
|
||||
await ctx.send(f"**Couldn't find repo:** `{repo_name}`")
|
||||
return
|
||||
|
||||
@@ -40,4 +42,24 @@ class Fun(commands.Cog):
|
||||
github_embed.add_field(name="Open Issues", value=repo_info['open_issues'], inline=True)
|
||||
github_embed.set_footer(text=f"Repo created at • {repo_info['created_at'].split('T')[0]}")
|
||||
|
||||
await ctx.send(embed=github_embed)
|
||||
await ctx.send(embed=github_embed)
|
||||
|
||||
"""
|
||||
@commands.command(usage="<member>")
|
||||
async def welcome(self, ctx, member: str = "") -> None:
|
||||
if len(member.strip()) == 0:
|
||||
await ctx.send("**Please provide a member mention. ex.** `!welcome @gcode`")
|
||||
return
|
||||
|
||||
GIPHY_API_KEY = os.getenv("GIPHY_API_KEY")
|
||||
|
||||
http_response = requests.get(f"https://api.giphy.com/v1/gifs/search?api_key={GIPHY_API_KEY}&q=welcome&limit=25&offset=0&rating=pg-13&lang=en")
|
||||
welcome_gifs = json.loads(http_response.text)
|
||||
welcome_gif = random.choice(welcome_gifs["data"])
|
||||
|
||||
gif_embed = discord.Embed()
|
||||
gif_embed.set_image(url=welcome_gif["images"]["original"]["url"])
|
||||
|
||||
await ctx.send(embed=gif_embed)
|
||||
await ctx.send(f"{member} **Welcome to {ctx.guild.name}!** :wave:")
|
||||
"""
|
||||
@@ -1,7 +1,7 @@
|
||||
from discord.ext import commands
|
||||
|
||||
class Python(commands.Cog):
|
||||
"""General commands about python programming."""
|
||||
"""Commands about python programming."""
|
||||
|
||||
@commands.command()
|
||||
async def resources(self, ctx) -> None:
|
||||
|
||||
23
pyvis.py
23
pyvis.py
@@ -1,5 +1,8 @@
|
||||
import discord
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
import random
|
||||
from cogs.python import Python
|
||||
from cogs.fun import Fun
|
||||
from cogs.finance import Finance
|
||||
@@ -12,13 +15,16 @@ load_dotenv()
|
||||
|
||||
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
|
||||
DISCORD_GUILD_NAME = os.getenv('DISCORD_GUILD_NAME')
|
||||
GIPHY_API_KEY = os.getenv("GIPHY_API_KEY")
|
||||
PREFIX = "!"
|
||||
|
||||
bot = commands.Bot(command_prefix=PREFIX)
|
||||
intents = discord.Intents.default()
|
||||
intents.members = True
|
||||
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
guild = discord.utils.find(lambda g: g.name == DISCORD_GUILD_NAME, bot.guilds)
|
||||
guild = discord.utils.find(lambda g: g.name == DISCORD_GUILD_NAME, bot.guilds) # TODO: check if find will throw an error
|
||||
if guild:
|
||||
print(
|
||||
f'{bot.user} is connected to the following guild:\n',
|
||||
@@ -27,7 +33,18 @@ async def on_ready():
|
||||
|
||||
@bot.event
|
||||
async def on_member_join(member):
|
||||
pass # TODO send new member welcome message on join
|
||||
guild = discord.utils.find(lambda g: g.name == DISCORD_GUILD_NAME, bot.guilds)
|
||||
welcome_channel = discord.utils.find(lambda c: c.name == "👋welcome", guild.channels)
|
||||
|
||||
http_response = requests.get(f"https://api.giphy.com/v1/gifs/search?api_key={GIPHY_API_KEY}&q=welcome&limit=25&offset=0&rating=pg-13&lang=en")
|
||||
welcome_gifs = json.loads(http_response.text)
|
||||
welcome_gif = random.choice(welcome_gifs["data"])
|
||||
|
||||
gif_embed = discord.Embed()
|
||||
gif_embed.set_image(url=welcome_gif["images"]["original"]["url"])
|
||||
|
||||
await welcome_channel.send(embed=gif_embed)
|
||||
await welcome_channel.send(f"<@!{member.id}> **Welcome to {DISCORD_GUILD_NAME}!** :wave:")
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
|
||||
Reference in New Issue
Block a user