Added dotenv library and some commands

This commit is contained in:
JKuijperM 2025-06-18 20:05:09 +02:00
parent de9a24e08a
commit aad81bbf06

107
bot.py
View File

@ -1,42 +1,121 @@
import os import os
from random import randrange
from dotenv import load_dotenv
from twitchio.ext import commands from twitchio.ext import commands
from twitchio.http import TwitchHTTP
from twitchio import PartialUser, Client
class Bot(commands.Bot): class Bot(commands.Bot):
count_to_hydrate = 0
count_to_prime_spam = 0
capital_warning = {}
def __init__(self): def __init__(self):
super().__init__(token=os.environ['TMI_TOKEN'], super().__init__(token=os.environ['TMI_TOKEN'],
# client_id=os.environ['CLIENT_ID'], client_id=os.environ['CLIENT_ID'],
nick=os.environ['BOT_NICK'], nick=os.environ['BOT_NICK'],
prefix=os.environ['BOT_PREFIX'], prefix=os.environ['BOT_PREFIX'],
initial_channels=[os.environ['CHANNEL']]) initial_channels=[os.environ['CHANNEL']])
self.client = Client(os.environ['TMI_TOKEN'])
self.http = TwitchHTTP(client=self.client, client_id=os.environ['CLIENT_ID'],
client_secret=os.environ['CLIENT_SECRET'])
async def event_ready(self): async def event_ready(self):
# We are logged in and ready to chat and use commands... # We are logged in and ready to chat and use commands...
print(f'Logged in as | {self.nick}') print(f'Logged in as | {self.nick}')
print(f'User id is | {self.user_id}') print(f'User id is | {self.user_id}')
await bot.connected_channels[0].send('/me El brujero ha llegado!')
async def event_message(self, message): await bot.connected_channels[0].send('/me {}'.format(os.environ['ARRIVAL']))
async def event_message(self, ctx, *, user=None):
# Messages with echo set to True are messages sent by the bot... # Messages with echo set to True are messages sent by the bot...
# For now we just want to ignore them... # For now we just want to ignore them...
if message.echo: if ctx.echo:
return return
# Print the contents of our message to console...
print(message.content)
# Since we have commands and are overriding the default `event_message` # Since we have commands and are overriding the default `event_message`
# We must let the bot know we want to handle and invoke our commands... # We must let the bot know we want to handle and invoke our commands...
await self.handle_commands(message) await self.handle_commands(ctx)
@commands.command() # Replies someone if have written the substring in the if
async def hello(self, ctx: commands.Context): if 'hola caracola' in ctx.content.lower():
# Send a hello back! await ctx.channel.send('Hola caracola @{}'.format(ctx.author.display_name))
await ctx.send('Hello world')
# For each 100 messages, send one
self.count_to_hydrate += 1
self.count_to_prime_spam += 1
if self.count_to_hydrate == 10:
self.count_to_hydrate = 0
await ctx.channel.send('/me Recordad hidrataros!!')
if self.count_to_prime_spam == 15:
self.count_to_prime_spam = 0
await ctx.channel.send(
'/me Recordad que si queréis apoyar el directo, podéis utilizar el prime en este canal.')
if ctx.content.isupper() and len(ctx.content) > 5:
# if not ctx.author.is_mod:
if ctx.author.name in self.capital_warning and self.capital_warning[ctx.author.name] < 4:
self.capital_warning[ctx.author.name] += 1
await ctx.channel.send(
'Menos gritos con los gritos @{}. Llevas con este {} avisos por gritar, al quinto '
'te ganas un pequeño timeout'.format(ctx.author.display_name,
self.capital_warning[ctx.author.name]))
elif ctx.author.name in self.capital_warning and self.capital_warning[ctx.author.name] >= 4:
await ctx.channel.send('Quien avisa no es traidor. @{}, el usuario @{} ha gritado varias veces.'.
format(ctx.channel.name, ctx.author.display_name))
# await ctx.channel.timeout(ctx.author.name, 5, "Escribir con mayúsculas varias veces")
# await ctx.channel.send('/timeout {} {} Spamear mayúsculas'.format(ctx.author.display_name, 300))
# ctx.author.channel
# TODO: time_out
# user_to_timeout = PartialUser(http=self.http, id=self.client.user_id, name=self.client.nick)
# await user_to_timeout.timeout_user(token=os.environ['TMI_TOKEN'],
# moderator_id=self.user_id,
# user_id=ctx.author.id, duration=5, reason="Spam de mayúsculas")
# await ctx.author.timeout_user(token=os.environ['TMI_TOKEN'], moderator_id=os.environ['CLIENT_ID'],
# user_id=ctx.author.id, duration=5, reason="Spam de mayúsculas")
else:
self.capital_warning[ctx.author.name] = 1
await ctx.channel.send('Menos gritos con los gritos @{}. Este es el primer aviso ten '
'cuidado con llegar a 5'.format(ctx.author.display_name))
@commands.command(name='redes')
async def social(self, ctx):
print('\nSending social media...')
await ctx.send('Twitter: {} || Instagram: {} || Discord: {} || Itch: {}'.format(os.environ['TWITTER'],
os.environ['INSTAGRAM'],
os.environ['DISCORD'],
os.environ['ITCHIO']))
@commands.command(name='placaje')
async def get_random_chatter(self, ctx):
# print(bot.connected_channels[0].chatters)
print("Tackling...")
list_chatters = list(bot.connected_channels[0].chatters)
random_indx = randrange(0, len(list_chatters) - 1)
if ctx.author.name == list_chatters[random_indx].name:
random_indx = (random_indx + 1) % (len(list_chatters) - 1)
if list_chatters[random_indx] is None:
await ctx.channel.send('{} has fallado el placaje y te caes al barro.'.format(ctx.author.display_name))
else:
await ctx.channel.send('{} manda a China a {} de un placaje'.format(ctx.author.display_name,
list_chatters[random_indx].display_name))
@commands.command(name='reco')
async def send_recommendation(self, ctx, arg):
print("Recommending channel...")
await ctx.channel.send('Pasad a echarle un ojo al canal de https://www.twitch.tv/{}'.format(arg))
@commands.command(name='comandos')
async def my_commands(self, ctx):
print('\nSending my command list...')
await ctx.channel.send('Los comandos disponibles ahora mismo son: !redes, !placaje, !reco. '
'También puedes saludar con un "Hola caracola".')
if __name__ == "__main__": if __name__ == "__main__":
load_dotenv()
bot = Bot() bot = Bot()
bot.run() bot.run()