Making Poketwo Autocatcher: Day 2 ish
Aight. I have decided that using machine learning is too difficult, so we will instead be using hints. To spice things up, we will be using multiple accounts at once!
First, you need to create a system to handle multiple discord clients.
First, you store named tuples in order to store channels to catch in as well as account tokens. So you do:
from collections import namedtuple
import client
Login = namedtuple('Login', 'token, catching_channels')
ACCOUNTS = [
Login(token = 'token', catching_channels = [channel_ID]),
Login(token = 'token', catching_channels = [channel_ID]),
Login(token = 'token', catching_channels = [channel_ID]),
]
SPAM_CHANNELS = [None]
Next, to handle multiple accounts, you do
import asyncio
from collections import namedtuple
import signal
import discord
import client
import config
entries = []
for Login in config.ACCOUNTS:
Entry = namedtuple('Entry', 'client, token')
newClient = client.MyClient(Login.catching_channels)
entries.append(
Entry(client = newClient, token = Login.token)
)
loop = asyncio.get_event_loop()
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
async def shutdown(signal, running_bots):
print(f"Received exit signal {signal.name}...")
[running_bot.cancel() for running_bot in running_bots]
await asyncio.gather(*running_bots, return_exceptions=True)
loop.stop()
async def wrapped_connect(entry):
try:
await entry.client.start(entry.token, bot = False)
print('We have logged in as {}'.format(entry))
finally:
print("Clean close of client")
await entry.client.close()
print('Client cleanly closed')
try:
running_bots = []
for entry in entries:
running_bots.append(loop.create_task(wrapped_connect(entry)))
for s in signals:
loop.add_signal_handler(s, lambda s = s: asyncio.create_task(shutdown(s, running_bots)))
loop.run_forever()
finally:
print('Program interruption')
loop.close()
Ok. Cool! Now that that is done, since we want to handle stuff within each client, we create out own custom client class to handle events for each account
import discord
from discord.ext import tasks
import random
import time
import config
from data import pokemon
poketwo_ID = 716390085896962058
class MyClient (discord.Client):
def __init__(self, allowed_channels):
super().__init__()
self.allowed_channels = allowed_channels
async def on_ready(self):
print("{} ready".format(self.user))
self.spam_channels = await self.get_channels()
self.spammer.start()
self.data = pokemon.DataManager()
async def get_channels(self):
channels = []
for channel_ID in config.SPAM_CHANNELS:
channels.append(self.get_channel(channel_ID))
return channels
@tasks.loop(seconds=3)
async def spammer (self):
channel = random.choice(self.spam_channels)
try:
await channel.send(' '.join(random.sample(string.ascii_letters+string.digits,50)*20))
except:
pass
async def on_message (self, message):
# do some catching stuff
We will be finishing up the real juicy stuff later! See you tmrw!! <3333